Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.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
Asp.net mvc ASP.NET MVC路由-“;“空白”;路线_Asp.net Mvc_Routing - Fatal编程技术网

Asp.net mvc ASP.NET MVC路由-“;“空白”;路线

Asp.net mvc ASP.NET MVC路由-“;“空白”;路线,asp.net-mvc,routing,Asp.net Mvc,Routing,我可以设置一个从根级别URL映射的路由吗 我正在使用VS2010内置的web服务器 尝试使用空白或单个斜杠URL字符串设置路由无效: routes.MapRoute( "Default", "/", new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); 它导致错误“路由URL不能以“/”或“~”字符开头,并且不能包含“?”字符

我可以设置一个从根级别URL映射的路由吗

我正在使用VS2010内置的web服务器

尝试使用空白或单个斜杠URL字符串设置路由无效:

routes.MapRoute(
    "Default",
    "/",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
它导致错误“路由URL不能以“/”或“~”字符开头,并且不能包含“?”字符。”。提前谢谢!我的整个路线定义如下:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "EditingTitles", // Route name
            "{controller}/{action}/{startingLetter}", // URL with parameters
            new { controller = "Admin", action = "Index", startingLetter = UrlParameter.Optional } // Parameter defaults
        );

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
    }

你想在这里实现什么。。。一个像这样的URL?因为如果是,则在未指定任何参数的情况下,默认路由将实现这一点

// Default Route:
routes.MapRoute(
   "Default", // Route name
   "{controller}/{action}/{id}", // URL with parameters
   new { controller = "Home", action = "Index", id = String.Empty } // Parameter defaults
);
使用ASPNET MVC5: RouteConfig.cs文件:

 public static void RegisterRoutes(RouteCollection routes)
 {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute(
        name: "Homepage",
        url: "",
        defaults: new { controller = "Content", action = "Index" }
    );
    routes.MapRoute(
        name: "foo",
        url: "bar",
        defaults: new { controller = "Content", action = "Index" }
    );
    routes.MapMvcAttributeRoutes();

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{title}",
        defaults: new { controller = "Content", action = "Details", title = UrlParameter.Optional }
    );
}
加:
如果您希望自动将主页重定向到另一个路由,如“到”,只需使用方法RedirectToRoute()

public class ContentController : Controller
    {
        public ActionResult Index()
        {
            return RedirectToRoute("foo");
        }
    }

你说得对,那个URL就是我想要实现的。我已经有一条和你指定的路线完全一样的路线了。但是当我F5我的项目时,我得到了一个HTTP 404(“找不到资源”)错误,它击中了默认的URL,所以我似乎必须以某种方式专门处理“空URL”的情况。你能将该路由粘贴到这里吗?听起来另一个指定的路由很糟糕,我有一个类似的问题,我的默认路由被覆盖,而不是处理根url。请发布问题中的所有路线。我建议使用Phil Haack的路线调试器:很好;我更新了问题以包括我的所有路线。我将查看发布的路由调试器。谢谢向我们展示您的其他路由,我遇到了与上述类似的问题(以及您在XSaint32答案上的注释),我的默认路由导致404,除非我指定了控制器/操作,这是由于我的路由中有一个命名错误的路由eslewhere