0

我有一个带有两个参数的动作,但是当调用动作时,参数在 url 中显示为查询字符串,如下所示:

localhost:34795/Verification?DepartmentID=3&SubDepartmentID=2

我知道使用自定义 url 路由我可以将其更改为:

本地主机:34795/验证/3/2

但我无法做到这一点,我将此代码添加到 Globas.asax 但直到“没有结果”

public class MvcApplication : System.Web.HttpApplication
{

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

        routes.MapRoute( "Blog", // Route name 
            "Verification/{DepartmentID}/{SubDepartmentID}", // URL with parameters
            new { controller = "Verification", action = "Index" } // Parameter defaults
            );

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

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


    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();
    }
}

我这样做了,但什么也没发生,我做错了什么?

4

2 回答 2

2

这应该可以解决问题:

routes.MapRoute("Blog",
    "Verification/{DepartmentID}/{SubDepartmentID}",
    new {
        controller = "Verification",
        action = "Index",
        DepartmentID = UrlParameter.Optional,
        SubDepartmentID = UrlParameter.Optional
    }
);

然后在IndexVerificationController 的函数中:

public ActionResult Index(int DepartmentID, int SubDepartmentID)
于 2013-08-04T03:31:00.117 回答
1

我得到了解决方案,经过彻底调试后,我意识到在 asp.net mvc 4 中,默认情况下它会在应用程序共享目录中创建一些类,并且用于注册路由的应用程序正在调用此方法:

protected void Application_Start()
    {

        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();
    }

您在这里看到它在 RouteConfig.cs 文件中调用 RegisterRoutes 方法,这意味着在 RouteConfig 类中它没有调用 Global.asax 的方法,这是问题所在,我很生气地解决了它,最后我得到了这个结果并得到了工作。

这是我的 RouteConfig.cs 代码,它解决了问题并使事情正常进行:

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

        routes.MapRoute("home", "", new { controller = "Home", action = "Index" });

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

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}
于 2013-08-05T03:33:53.130 回答