亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

ASP.NET Core MVC解決控制器同名Action請求不明確的問題

 更新時間:2021年03月06日 10:27:39   作者:zzx  
這篇文章主要介紹了ASP.NET Core MVC解決控制器同名Action請求不明確的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

在Asp.Net Core MVC Web應(yīng)用程序的開發(fā)過程當(dāng)中,如果需要在控制器內(nèi)使用同名的Action,則會出現(xiàn)如下圖所示的問題:

https://docs.microsoft.com/zh-cn/aspnet/core/mvc/controllers/routing?view=aspnetcore-5.0
代碼片段如下:

` //GET: /HelloWorld/Welcome
public string Welcome()
{
return "這是HelloWorld控制器下的Welcome Action方法.....";
}
//帶參數(shù)的Action
  //GET: /HelloWorld/Welcome?name=xxxx&type=xxx
  public string Welcome(string name, int type)
  {
    //使用Http Verb謂詞特性路由模板配置解決請求Action不明確的問題
    //AmbiguousMatchException: The request matched multiple endpoints. Matches:
    //[Controller]/[ActionName]/[Parameters]
    //中文字符串需要編碼
    //type為可解析為int類型的數(shù)字字符串
    string str = HtmlEncoder.Default.Encode($"Hello {name}, Type is: {type}");
    return str;
  }`

只要在瀏覽器的Url地址欄輸入"/HelloWorld/Welcome"這個路由地址段時,Asp.Net Core的路由解析中間件便拋出上圖所示的請求操作不明確的問題。
根據(jù)官方文檔的描述,可以在控制器內(nèi)某一個同名的Action方法上添加HTTP Verb Attribute特性的方式(為此方法重新聲明一個路由Url片段)來解決此問題。對HelloWorld控制器內(nèi),具有參數(shù)的"Welcome"這個Action添加HTTPGetAttr
修改后的代碼如下:

//帶參數(shù)的Action
//GET: /HelloWorld/Welcome?name=xxxx&type=xxx
[HttpGet(template:"{controller}/WelcomeP", Name = "WelcomeP")]
public string Welcome(string name, int type)
{
string str = HtmlEncoder.Default.Encode($"Hello {name}, Type is: {type}");
return str;
}

請求Url: Get -> "/HelloWorld/Welcome?name=xxxxx&type=0"

到此這篇關(guān)于ASP.NET Core MVC解決控制器同名Action請求不明確的問題的文章就介紹到這了,更多相關(guān)ASP.NET Core MVC控制器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論