Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 未定义url参数时重定向到主页的Asp.Net路由_C#_Asp.net_Asp.net Mvc_Asp.net Routing - Fatal编程技术网

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" }
        }
}