簡體   English   中英

我的ASP .NET MVC網站上的URL重寫

[英]URL Rewriting on my ASP .NET MVC website

我正在使用圖像查看器,因此我有一個名為Viewer的控制器,當我將其傳遞給routeValues它會通過以下URL傳遞: http : routeValues =2

這是進入此頁面的鏈接:

@Url.Action("Index", new { category = p.Category, image = p.Image })

但我確實希望擁有此URL: http : //www.mywebsite.com/Viewer/1/2

我試圖在RouteConfig類的RegisterRoutes方法中做一些技巧,但無法獲得先前的結果。

public class RouteConfig
{
    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: "Viewer",
            url: "{controller}/{action}/{category}-{image}",
            defaults: new { controller = "Viewer", action = "Index", category = 1, image = 1 }
        );
    }
}

有人知道我在哪里可以做嗎?

非常感謝 !

您需要將更具體的路由放在默認路由之前,因為路由的評估順序與聲明它們的順序相同:

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

        routes.MapRoute(
            name: "Viewer",
            url: "{controller}/{action}/{category}/{image}",
            defaults: new { controller = "Viewer", action = "Index", category = 1, image = 1 }
        );

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM