2017-04-10 50 views
0

我有两个途径Asp.Net路由会被重定向URL,如果参数没有定义

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

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

当用户进入主页面,他已经选择了两个环节:

<a href="/loc1/Products/index">Location 1</a> 
<a href="/loc2/Products/index">Location 2</a> 

而且我有ActionFilterAttribute,我在那里检查location参数,如果url没有,我将用户重定向到主页面。

public override void OnActionExecuting(ActionExecutingContext filterContext) 
{ 
    string location = (string)filterContext.RouteData.Values["location"]; 
    if (string.IsNullOrEmpty(location) 
     && filterContext.ActionDescriptor.ControllerDescriptor.ControllerName != "Home" 
     && !filterContext.IsChildAction) 
    { 
     filterContext.Result = new RedirectToRouteResult(
     new RouteValueDictionary 
     { 
      { "controller", "Home" }, 
      { "action", "Index" } 
     }); 
    } 

    SessionUtils.SetLocationName(location); 
} 

基础上location可变我会查询数据库中不同的记录,所以当用户没有提供网址的任何位置,并尝试访问Products/index/5我希望他们被重定向到他们的主页选择位置并单击其中一个链接。 但是,我收到错误消息:

无法找到该资源。

说明:HTTP 404。您正在寻找(或它的一个依赖)的资源可能已被删除,更名或暂时不可用。请检查以下网址并确保它拼写正确。

请求的URL:/产品/指数

我应该如何正确地进行路由配置,让用户将被重定向到主页如果网址中没有位置参数?

回答

0

综合了所有的答案,并特别说明,我想出了这个配置

路由配置:

routes.MapRoute(
    name: "DefaultShort", 
    url: "{location}/", 
    defaults: new { controller = "Products", action = "Index" } 
); 

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

routes.MapRoute(
    name: "DefaultHome", 
    url: "{controller}/{action}/", 
    defaults: new { controller = "Home", action = "LandingPage"} 
); 

OnActionExecuting

if (string.IsNullOrEmpty(location) 
    && cookie != null 
    && !string.IsNullOrEmpty(cookie.Values["location"]) 
    && filterContext.ActionDescriptor.ActionName == "LandingPage" 
    && filterContext.ActionDescriptor.ControllerDescriptor.ControllerName == "Home") 
{ 
    filterContext.Result = new RedirectToRouteResult(
     new RouteValueDictionary 
     { 
      { "location", cookie.Values["location"]}, 
      { "controller", "Measurements" }, 
      { "action", "Index" } 
     } 
    ); 
} 
else if (string.IsNullOrEmpty(location) 
    && filterContext.ActionDescriptor.ControllerDescriptor.ControllerName != "Home" 
    && !filterContext.IsChildAction) 
{ 
    filterContext.Result = new RedirectToRouteResult(
     new RouteValueDictionary 
     { 
      { "controller", "Home" }, 
      { "action", "LandingPage" } 
     } 
} 

如果只提供位置有些逻辑,那么它将使用第一条路线,该路线将重定向到Products控制器。

如果全部三个参数在路线中匹配,则将使用第二条路线location,controlleraction。我没有提供默认controlleraction。当重定向到HomeController时,这允许使用最后的路线。

第三条路线将有两个参数 - controlleraction,它允许我在HomeController中进入默认视图。使用OnActionExecuting如果未指向HomeControllerLandingPage,我将重定向到此默认视图和控制器。

0

尝试Default路线

routes.MapRoute(
    name: "Location1Route", 
    url: "{location}/{controller}/{action}", 
    defaults: new { controller = "Home", action = "Index" } 
); 

,这将满足您的需要之前添加自定义route

+0

如果我不想依赖'loc1'和'loc2'会怎么样?实际的“位置”将从数据库中拉出,HTML将自动渲染。 –

+0

编辑符合您需要的答案 – manish

1

您的“默认”路线,其中包括位置是唯一的路线,将被击中。该controlleractionid PARAMS都是可选的,所以这样的事情/Products/Index请求实际上由你的“默认”的路线被捕获,并且location PARAM将给予“产品”和你controller PARAM将给予“指数”为一个值。 action参数将是“索引”的默认值,而id参数将被忽略。既然你显然不具备的IndexControllerIndex动作,你会得到一个404

为了区分路由,需要需要让你的“默认”的路线需要某种硬编码的前缀,即

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

然后,/Products/Index路线将落入您的“家”路线,并被正确解释为到ProductsController.Index的路线。另外,您也可以申请航线约束上的location PARAM,所以它不匹配一切,即:

routes.MapRoute(
    name: "Default", 
    url: "{location}/{controller}/{action}/{id}", 
    defaults: new { controller = "Products", action = "Index", id = UrlParameter.Optional }, 
    constraints: new { location = @"^(loc1|loc2)$" } 
    ); 

然后,除非URL的第一部分相匹配或者loc1loc2,它都将落空到“家”路线。

+0

当我进入主页面时使用一个'Default'路由我得到'HTTP Error 403.14 - Forbidden'。是否有可能去主页没有ptoviding位置参数? –

0

您是否考虑过硬编码默认值和故障安全路由?

routes.MapRoute("Home", "", new { controller = "Home", action = "Index" }); 
/*  Normal routing logic here  */ 
routes.MapRoute("FailSafe", "{*url}", new { controller = "Home", action = "Index"}); // !=Last Line=!