简体   繁体   English

ASP.NET MVC4路由-到同一位置的多条路由

[英]ASP.NET MVC4 Routing - Multiple routes to the same location

I am in the process of setting up a Single Page Application (SPA) and would like to setup, currently two routes. 我正在设置单页应用程序(SPA),并且想设置当前两条路由。 For instance: 例如:

  • Route 1: http://localhost - this is the default route which requires authentication (Admin area) 路由1: http://localhost这是需要身份验证的默认路由(管理区域)
  • Route 2: http://localhost/<client>/<clients project name>/ - this does not require authentication (view only) 路线2: http://localhost/<client>/<clients project name>/ -不需要身份验证(仅查看)

In the admin area, they setup the <client> and <clients project name> , therefore I know I need to setup this configuration in MVC4 Routes, but it is unclear to me how I would approach this. 在管理区域中,他们设置了<client><clients project name> ,因此我知道我需要在MVC4路由中设置此配置,但是我不清楚我将如何处理它。

Another caveat would be, if the <clients project name> was not entered into the URL, it would present a search page for that client. 另一个警告是,如果未在URL中输入<clients project name> ,它将显示该客户端的搜索页面。

One of the great things about routing in MVC is the ability to route anything to anywhere, regardless of whether the url matches the naming of controllers and action methods. 关于MVC中路由的一大优点是能够将任何内容路由到任何地方,而无论url是否与控制器和操作方法的名称匹配。 The RouteConfig allows us to register specific routes to cater for this. RouteConfig允许我们注册特定的路由来满足此需求。 Let me show you how you can achieve this. 让我向您展示如何实现这一目标。

Route 1: 路线1:

This is handled by the default route in the route config. 这由路由配置中的默认路由处理。

routes.MapRoute(
    name: "Default", 
    url: "{controller}/{action}/{id}", 
    defaults: new { controller = "Home", 
                    action = "Index", 
                    id = UrlParameter.Optional });

Hitting http://localhost will take you to the Home controller and the Index action method. 点击http://localhost将带您进入Home控制器和Index操作方法。

Route 2: 路线2:

We can set up one route that will cater for http://localhost/<client> and http://localhost/<client>/<clients project name> 我们可以设置一条路由来满足http://localhost/<client>http://localhost/<client>/<clients project name>

routes.MapRoute(
    "Client", 
    "{client}/{title}", 
    new { controller = "Home", 
          action = "Client", 
          title = UrlParameter.Optional });

Hitting either http://localhost/bacon or http://localhost/bacon/smokey will take you to the Home controller and the Client action method. 点击http://localhost/baconhttp://localhost/bacon/smokey将带您进入Home控制器和Client操作方法。 Notice the title is an optional parameter this is how we can get both urls to work with the same route. 注意title是一个可选参数,这是我们如何使两个URL都可以在同一路由下工作的方式。

For this to work on the controller end our action method Client would need to look like this. 为了使它在控制器端起作用,我们的操作方法Client需要看起来像这样。

public ActionResult Client(string client, string title = null)
{
    if(title != null)
    {
       // Do something here.
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM