Jes*_*sse 2 asp.net-mvc asp.net-mvc-routing c#-4.0 asp.net-mvc-4 single-page-application
我正在设置单页面应用程序(SPA)并且想要设置,目前有两条路线.例如:
http://localhost
- 这是需要身份验证的默认路由(管理区域)http://localhost/<client>/<clients project name>/
- 这不需要验证(仅限查看)在管理方面,他们建立了<client>
和<clients project name>
,因此我知道我需要设置在MVC4路线此配置,但我不清楚我会怎么处理这个.
另一个警告是,如果<clients project name>
未输入URL,它将为该客户端提供搜索页面.
在MVC中进行路由的一个好处是能够将任何内容路由到任何地方,无论url是否与控制器和操作方法的命名相匹配.RouteConfig允许我们注册特定路线以满足此需求.让我告诉你如何实现这一目标.
路线1:
这由路由配置中的默认路由处理.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index",
id = UrlParameter.Optional });
Run Code Online (Sandbox Code Playgroud)
点击http://localhost
将带您进入Home
控制器和Index
操作方法.
路线2:
我们可以设置一条适合http://localhost/<client>
和的路线http://localhost/<client>/<clients project name>
routes.MapRoute(
"Client",
"{client}/{title}",
new { controller = "Home",
action = "Client",
title = UrlParameter.Optional });
Run Code Online (Sandbox Code Playgroud)
点击其中一个http://localhost/bacon
或http://localhost/bacon/smokey
将带您到Home
控制器和Client
操作方法.请注意,这title
是一个可选参数,这是我们如何使两个URL使用相同的路由.
为了在控制器端工作,我们的action方法Client
需要看起来像这样.
public ActionResult Client(string client, string title = null)
{
if(title != null)
{
// Do something here.
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7452 次 |
最近记录: |