System.Web.Mvc.HttpPostAttribute vs System.Web.Http.HttpPostAttribute? [duplicate]

Posted Chuck Lu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了System.Web.Mvc.HttpPostAttribute vs System.Web.Http.HttpPostAttribute? [duplicate]相关的知识,希望对你有一定的参考价值。

System.Web.Mvc.HttpPostAttribute vs System.Web.Http.HttpPostAttribute? [duplicate]

回答1

Prior to ASP.NET Core, MVC and WebAPI were mainly separate libraries.

The .Mvc namespace applies to MVC controllers, the .Http namespace to Web API controllers.

 

What is the different between System.Web.Http.HttpPut Vs System.Web.Mvc.HttpPut

They belong to two different frameworks. The pipeline flow of each is looking for specific attributes that belong to their respective namespaces.

The routing engine for the respective frameworks are not aware of the other so if a Web API attribute is used on a MVC action it would be just as if there were no attribute at all, hence the 405 error encountered.

Make sure that the correct namespace is used on the correct controller type. If both namespaces are being used in the file then be specific by calling [System.Web.Http.HttpPut] for Web API actions

[System.Web.Http.HttpPut]
public IHttpActionResult Put([FromBody]MyModel model) { return Ok(); }

and [System.Web.Mvc.HttpPut] for MVC actions

[System.Web.Mvc.HttpPut]
public ActionResult Put([FromBody]MyModel model) { return View(); }

 

 

理解ASP.NET Core

注:本文隶属于《理解ASP.NET Core》系列文章,请查看置顶博客或点击此处查看全文目录 前言 在.NET中,我们有很多发送Http请求的手段,如HttpWebRequest、WebClient以及HttpClient。 在进入正文之前,先简单了解一下前2个: HttpWebRequest na

以上是关于System.Web.Mvc.HttpPostAttribute vs System.Web.Http.HttpPostAttribute? [duplicate]的主要内容,如果未能解决你的问题,请参考以下文章