混合ASP.NET和MVC路由

时间:2020-12-31 03:21:42

I thought I could have friendly URLs for all routes in my mixed ASP.NET + MVC application, but it is not working as I expect. Here is my routing definition setup:

我以为我可以为我的混合ASP.NET + MVC应用程序中的所有路由提供友好的URL,但它没有按照我的预期工作。这是我的路由定义设置:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapPageRoute("Design-Fancy", "Design/Fancy/{*queryvalues}", "~/Design/example10.aspx", true);
    routes.MapPageRoute("Design-Simple", "Design/Simple/{*queryvalues}", "~/Design/example5.aspx", true);

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

While this works to route to the *.aspx pages, Any Razor action tags on the same page that are defined for example as "Home" for the controller and "About" for the Action actually are rendered in the page source as 'http://..../Design/Fancy?action=About&controller=Home'. So, this breaks all the navigation menu URLs, etc. I must be doing it wrongly!

虽然这可以路由到* .aspx页面,但是在同一页面上为控制器定义为“Home”并且Action的“关于”的任何Razor动作标签实际上在页面源中呈现为'http: //..../Design/Fancy?action=About&controller=Home”。因此,这打破了所有导航菜单URL等。我必须做错了!

1 个解决方案

#1


0  

Here is the solution I settled on. I installed the NuGet Microsoft.AspNet.FriendlyUrls package. Then I named the .aspx page with a page name that would look good without the extension. Then I set up the routing as follows:

这是我解决的解决方案。我安装了NuGet Microsoft.AspNet.FriendlyUrls包。然后我将.aspx页面命名为页面名称,该页面在没有扩展名的情况下看起来很好。然后我按如下方式设置路由:


    public static void RegisterRoutes(RouteCollection routes)
    {
        FriendlyUrlSettings aspxSettings = new FriendlyUrlSettings();
        aspxSettings.AutoRedirectMode = RedirectMode.Off;  // default=Off
        routes.EnableFriendlyUrls(aspxSettings);

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

        routes.MapPageRoute("Design-Fancy", "Design/Fancy/{*queryvalues}", "~/Design/Fancy.aspx", true);
        routes.MapPageRoute("Design-Simple", "Design/Simple/{*queryvalues}", "~/Design/Simple.aspx", true);
    } 

This gives the effect I wanted, and works with my MVC routing, and results in routing to .aspx pages while removing the .aspx extension.

这提供了我想要的效果,并与我的MVC路由一起使用,并导致在删除.aspx扩展名的同时路由到.aspx页面。

#1


0  

Here is the solution I settled on. I installed the NuGet Microsoft.AspNet.FriendlyUrls package. Then I named the .aspx page with a page name that would look good without the extension. Then I set up the routing as follows:

这是我解决的解决方案。我安装了NuGet Microsoft.AspNet.FriendlyUrls包。然后我将.aspx页面命名为页面名称,该页面在没有扩展名的情况下看起来很好。然后我按如下方式设置路由:


    public static void RegisterRoutes(RouteCollection routes)
    {
        FriendlyUrlSettings aspxSettings = new FriendlyUrlSettings();
        aspxSettings.AutoRedirectMode = RedirectMode.Off;  // default=Off
        routes.EnableFriendlyUrls(aspxSettings);

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

        routes.MapPageRoute("Design-Fancy", "Design/Fancy/{*queryvalues}", "~/Design/Fancy.aspx", true);
        routes.MapPageRoute("Design-Simple", "Design/Simple/{*queryvalues}", "~/Design/Simple.aspx", true);
    } 

This gives the effect I wanted, and works with my MVC routing, and results in routing to .aspx pages while removing the .aspx extension.

这提供了我想要的效果,并与我的MVC路由一起使用,并导致在删除.aspx扩展名的同时路由到.aspx页面。