C# 未定义url参数时重定向到主页的Asp.Net路由
我有两条路线C# 未定义url参数时重定向到主页的Asp.Net路由,c#,asp.net,asp.net-mvc,asp.net-routing,C#,Asp.net,Asp.net Mvc,Asp.net Routing,我有两条路线 routes.MapRoute( name: "Default", url: "{location}/{controller}/{action}/{id}", defaults: new { controller = "Products", action = "Index", id = UrlParameter.Optional } ); routes.MapRoute
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>
基于location
变量,我将从数据库中查询不同的记录,因此当用户在url中不提供任何位置,并尝试访问Products/index/5
时,我希望他们被重定向到主页面,在主页面上他们必须选择位置并单击其中一个链接。
但是,我收到错误消息:
找不到资源
描述:HTTP404。您正在查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。请检查以下URL并确保其拼写正确
请求的URL:/产品/索引
我应该如何正确配置路由,以便在url中没有位置参数时将用户重定向到主页面?尝试在
默认路由之前添加自定义路由
routes.MapRoute(
name: "Location1Route",
url: "{location}/{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
这将符合您的需要。您的“默认”路线(包括位置)是唯一会被击中的路线。controller
、action
和id
参数都是可选的,因此对/Products/Index
之类的请求实际上会被您的“默认”路由捕获,位置
参数将被赋予“Products”,而controller
参数将被赋予“Index”作为值。action
param将是“Index”的默认值,而id
param将被忽略。由于显然没有带有索引
操作的IndexController
,因此得到的是404
要区分路由,您需要使“默认”路由需要某种硬编码前缀,即
routes.MapRoute(
name: "Default",
url: "location/{location}/{controller}/{action}/{id}",
defaults: new { controller = "Products", action = "Index", id = UrlParameter.Optional }
);
然后,/Products/Index
路线将进入您的“主页”路线,并被正确解释为通往ProductsController.Index
的路线。或者,您可以将路由约束应用于位置
参数,使其不匹配所有内容,即:
然后,除非URL的第一部分与loc1
或loc2
匹配,否则它将进入“主”路由。您是否考虑过硬编码默认路由和故障安全路由
routes.MapRoute("Home", "", new { controller = "Home", action = "Index" });
/* Normal routing logic here */
routes.MapRoute("FailSafe", "{*url}", new { controller = "Home", action = "Index"}); // !=Last Line=!
结合所有答案,特别是解释,我得出了这个配置
路由配置:
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" }
}
}
如果只提供位置,则它将使用第一条路线,该路线将重定向到产品
控制器
如果路线中的三个位置
、控制器
和动作
参数均匹配,则将使用第二条路线。我没有提供默认的控制器
和操作
。这允许在重定向到HomeController
时使用最后一条路由
第三条路线将有两个参数-controller
和action
,这允许我在HomeController
中进入默认视图。使用OnActionExecuting
我将重定向到此默认视图和控制器,如果它不指向HomeController
和LandingPage
如果我不想依赖loc1
和loc2'?实际的
location`将从数据库中提取,HTML将自动呈现。当我转到主页时,使用一个默认
路由,我得到HTTP错误403.14-禁止
。是否可以在不提供位置参数的情况下转到主页面?
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"}
);
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" }
}
}