亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

帶有 Action/Id 的 ASP.net 自定義路由

帶有 Action/Id 的 ASP.net 自定義路由

C#
白豬掌柜的 2023-04-16 09:52:41
在過去的幾個小時里,我一直在嘗試更改我網站的路由,但我就是找不到 ASP.net 想要從我這里得到什么!這是我的默認路由:        routes.MapRoute(            name: "Default",            url: "{controller}/{action}/{Id}",            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, }        );每當我訪問這些 URL 時,它都會成功地顯示 Home/Index://Home/Home/Index但是,我有一個名為“K”的操作,如下所示,它接收一個參數“name”:public ActionResult K(string name)我想/Home/K/{name}用這個模板定義一個將我重定向到的路由:website.com/K/{name}我在下面嘗試了這條路線,但它不起作用:routes.MapRoute(            "OnlyK",            "K/{id}",            new { controller = "Home", action = "K", id = UrlParameter.Optional }        );即使沒有此路由配置,如果我去website.com/Home/K/something它也不會將“某物”識別為 id(控制器參數 == null)!我究竟做錯了什么?
查看完整描述

1 回答

?
holdtom

TA貢獻1805條經驗 獲得超10個贊

由于參數名稱不匹配,urlwebsite.com/Home/K/something會導致操作方法中的null參數值。 該方法通過 route 提供服務,該 route 聲明了一個名稱為(via )的參數,而 action 方法有一個名為 的參數。nameKActionResult K(string name)

Defaultid{controller}/{action}/{id}Kname


您可以通過否決操作方法上的參數名稱來解決此問題,以便BindAttribute兩者匹配。


ActionResult K([Bind(Prefix ="id")] string name)

要從到重定向,website.com/K/{name}您website.com/Home/K/{name}可以設置一個自定義IRouteHandler來處理此重定向。


注冊一個處理任何與路由匹配的請求的路由K/{id},并將這些請求重定向到具有眾所周知名稱的路由(此處:)K。

由于討論的參數名稱不匹配,我們將在路由中使用{id}instead of 。{name}


routes.Add(new Route("K/{id}", new RedirectRouteHandler("K")));

K如下定義這條路線。


routes.MapRoute(

    name: "K",

    url: "Home/K/{id}",

    defaults: new { controller = "Home", action = "K", id = UrlParameter.Optional }

    );

并RouteHandler進行RedirectHandler重定向。

該類HttpResponse有一個RedirectToRoute方法可以處理路由名稱和路由值,而無需我們自己構建 url。


class RedirectRouteHandler : IRouteHandler

{

    private readonly string _routeName;


    public RedirectRouteHandler(string routeName)

    {

        _routeName = routeName;

    }


    public IHttpHandler GetHttpHandler(RequestContext requestContext)

    {

        return new RedirectHandler(this._routeName, requestContext.RouteData.Values);

    }

}


class RedirectHandler : IHttpHandler

{

    private readonly string _routeName;

    private readonly RouteValueDictionary _routeValues;


    public RedirectHandler(string routeName, RouteValueDictionary routeValues)

    {

        this._routeName = routeName;

        this._routeValues = routeValues;

    }


    public bool IsReusable { return false; }


    public void ProcessRequest(HttpContext context)

    {

        context.Response.RedirectToRoute(this._routeName, this._routeValues);

    }

}

請注意,注冊路線的順序很重要;RegisterRoutes好像。


public static void RegisterRoutes(RouteCollection routes)

{

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


    routes.Add(new Route("K/{id}", new RedirectRouteHandler("K")));


    routes.MapRoute(

        name: "K",

        url: "Home/K/{id}",

        defaults: new { controller = "Home", action = "K", id = UrlParameter.Optional }

        );


    routes.MapRoute(

        name: "Default",

        url: "{controller}/{action}/{id}",

        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

    );

}


查看完整回答
反對 回復 2023-04-16
  • 1 回答
  • 0 關注
  • 175 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號