久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

帶有兩個可選參數的asp mvc路由

asp mvc routing with two optional parameters(帶有兩個可選參數的asp mvc路由)
本文介紹了帶有兩個可選參數的asp mvc路由的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

您好,當兩個參數都是可選的時,我如何映射 url ../Companies/Results/value/id?

Hi How do I map the url ../Companies/Results/value/id when both the parameters are optional?

Companies 是控制器,Results 是操作,value 和 id 是可選參數.在我的表單上是一個值的文本框和一個 id 的選擇列表.用戶可以選擇兩者或其中之一進行搜索.嘗試過這樣的事情,但是當缺少可選參數之一(例如值)時無法處理,例如 ../Companies/Results//id

Companies is the controller, Results is the action, value and id are optional parameters. On my form is a textbox for value and a selectlist for an id. The user could select both or one of each to search by. Tried something like this but cant handle when one of the optional parameters, say value, is missing such as ../Companies/Results/ /id

        routes.MapRoute(
            "Company+Profession", // Route name
            "{action}/{value}/{profId}", // URL with parameters
            new { controller = "Companies", action = "Index", value = UrlParameter.Optional, profId = UrlParameter.Optional } // Parameter defaults
        );

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

推薦答案

你不能有一個有兩個可選參數的路由,因為你描述的問題,只有最后一個參數可以是可選的.我建議您為 value 設置一個默認參數,例如 byid,并在人們選擇專業時使用它.

You can't have a route that has two optional parameters, only the last parameter can be optional precisely because of the problem you describe. I suggest that you have a default parameter for value, like byid and use this when the person selects a profession.

我假設您是通過 javascript 構建 URL,因為使用 GET 表單操作會導致將參數名稱添加到 URL.在這種情況下,當文本框為空時,只需插入默認的 byid.

I assume that you're constructing the URL via javascript, since using a GET form action would result in the parameter names being added to the URL. In this case, when the textbox is empty simply insert the default byid.

更新您的路由以包含默認值,以便您生成的任何 URL 都可以使用.請參閱 Phil Haack 的 博文 關于這一點的另一種方法來處理生成具有兩個可選"參數的 URL.

Update your route to include the default so any URLs that you generate will work. See Phil Haack's blog post on this for an alternative way to handle generating URLs that have two "optional" parameters.

// used when both parameters are specified
routes.MapRoute(
        "Company+Profession", // Route name
        "{action}/{value}/{profId}", // URL with parameters
        new { controller = "Companies", action = "Index", value ="byid", profId = UrlParameter.Optional } // Parameter defaults
);

這篇關于帶有兩個可選參數的asp mvc路由的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 進行身份驗證并跨請求保留自定義聲明)
ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授權不起作用)
ASP Core Azure Active Directory Login use roles(ASP Core Azure Active Directory 登錄使用角色)
How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何獲取守護進程或服務器到 C# ASP.NET Web API 的 Azure AD OAuth2 訪問令牌和刷新令牌) - IT屋-程序員軟件開發技
.Net Core 2.0 - Get AAD access token to use with Microsoft Graph(.Net Core 2.0 - 獲取 AAD 訪問令牌以與 Microsoft Graph 一起使用)
Azure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(異步調用時 Azure KeyVault Active Directory AcquireTokenAsync 超時)
主站蜘蛛池模板: 天天躁日日躁性色aⅴ电影 免费在线观看成年人视频 国产欧美精品 | 久久久久久国产精品mv | 久久久蜜桃 | 国产一区二区三区四区区 | 久久久久国产精品一区二区 | 久久y| 中文字幕二区 | 亚洲a视频| 亚洲综合视频一区 | 2020亚洲天堂 | 精品一区二区在线观看 | 日韩高清一区 | 狠狠色综合欧美激情 | 成在线人视频免费视频 | 欧美日韩久久精品 | 国产成人精品免费 | 一区二区三区观看视频 | 日韩成年人视频在线 | 国产成人精品久久二区二区91 | 天天操欧美 | 日韩精品在线一区 | 国产激情一区二区三区 | 天天操网 | 久久精品伊人 | 天天干夜夜拍 | 日日夜夜av | 久久久蜜臀国产一区二区 | 亚洲欧洲激情 | 午夜国产一区 | 国产成人亚洲精品自产在线 | 龙珠z在线观看 | 97视频在线免费 | 午夜视频一区 | 正在播放国产精品 | 91香蕉视频在线观看 | 欧美综合色 | 国产精品入口久久 | 国产精品福利在线 | 91在线一区| 国产福利在线视频 | 中文二区|