无法在MVC5 Web应用程序中创建正确的路由

时间:2019-04-14 05:24:15

标签: c# asp.net-mvc-5 routing

我正在尝试为我的Web应用程序的操作之一创建路由,但是我无法这样做。这是当前网址:    http://localhost:8093/Profile/Get?Name=John 我想更改为   http://localhost:8093/Profile/John

在我的RouteConfig中,我尝试过

routes.MapRoute(
             name: "Profile",
             url: "Profile/Get/{Name}",
             defaults: new { controller = "Profile", action = "Get", Name = UrlParameter.Optional }
        );

routes.MapRoute(
             name: "Profile",
             url: "Profile/{Name}",
             defaults: new { controller = "Profile", action = "Get", Name = UrlParameter.Optional }
        );

但是没有任何效果。关于我在做什么错需要一些指导。

这是RegisterRoutes方法

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


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

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

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

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

    }

routes.MapRoute(
         name: "Profile",
         url: "Profile/{Name}",
         defaults: new { controller = "Profile", action = "Get", Name = UrlParameter.Optional }
    );

2 个答案:

答案 0 :(得分:0)

您可以使用此名称并更改您的自定义控制器名称和操作名称:

GithubRepoAdapter

您不需要在网址设置中使用查询字符串。

答案 1 :(得分:0)

您的问题是default路线,应该总是在最后。 RouteConfig.cs文件是从上到下读取的,将转到找到与URL匹配的第一个路径。请将您的RouteConfig.cs更改为以下给定的值。

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

    routes.MapRoute(
         name: "Profile",
         url: "Profile/{Name}",
         defaults: new { controller = "Profile", action = "Get", Name = UrlParameter.Optional }
    );

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

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

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

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

}