最近很多小伙伴都在问asp.net-mvc-3–WebAPIPUT/POST中的部分实体更新和asp.netupdatepanel用法这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓
最近很多小伙伴都在问asp.net-mvc-3 – WebAPI PUT / POST中的部分实体更新和asp.net updatepanel用法这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展ASP.NET MVC WebAPI Put和Delete请求出现405(Method not allowed)错误、ASP.NET MVC- 在现有 MVC 项目添加 WebAPI【转】、ASP.NET MVC4 WebAPI和Posting XML数据、asp.net-mvc – ASP.NET MVC 4 Web Api和REST经典服务之间的区别等相关知识,下面开始了哦!
本文目录一览:- asp.net-mvc-3 – WebAPI PUT / POST中的部分实体更新(asp.net updatepanel用法)
- ASP.NET MVC WebAPI Put和Delete请求出现405(Method not allowed)错误
- ASP.NET MVC- 在现有 MVC 项目添加 WebAPI【转】
- ASP.NET MVC4 WebAPI和Posting XML数据
- asp.net-mvc – ASP.NET MVC 4 Web Api和REST经典服务之间的区别
asp.net-mvc-3 – WebAPI PUT / POST中的部分实体更新(asp.net updatepanel用法)
public Document UpdateDocument(Document document) { Document serverDocument = _db.Documents.Find(document.Id); serverDocument.Title = document.Title; serverDocument.Content = document.Content; _db.SaveChanges(); return serverDocument; }
在这种情况下,实体有两个属性.更新文档时,JSON请求中都需要这两个属性,因此请求PUT / api /文件夹与一个
{ "documentId" = "1","title" = "Updated Title" }
将返回错误,因为没有提供“内容”.我这样做的原因是因为即使用户不能更新的可空属性和属性,强制客户端在请求中指定这些字段似乎更安全,以避免用空服务器覆盖未指定的字段.
这导致我在PUT和POST请求中始终要求每个可更新属性的做法,即使这意味着为这些属性指定null.
这是很酷,还是有一个我还没有学到的模式/实践,这可能通过发送只需要通过电线来促进部分更新?
解决方法
事实上,像你这样的用例是IETF首先介绍它的原因.
RFC 5789定义非常精确:
PATCH is used to apply partial modifications to a resource.
A new method is necessary to improve interoperability and prevent
errors. The PUT method is already defined to overwrite a resource
with a complete new body,and cannot be reused to do partial changes.
Otherwise,proxies and caches,and even clients and servers,may get
confused as to the result of the operation. POST is already used but
without broad interoperability (for one,there is no standard way to
discover patch format support).
Mark Nottingham在API设计 – http://www.mnot.net/blog/2012/09/05/patch中写了一篇关于使用PATCH的伟大文章
在你的情况下,那将是:
[AcceptVerbs("PATCH")] public Document PatchDocument(Document document) { Document serverDocument = _db.Documents.Find(document.Id); serverDocument.Title = document.Title; serverDocument.Content = document.Content; _db.SaveChanges(); return serverDocument; }
ASP.NET MVC WebAPI Put和Delete请求出现405(Method not allowed)错误
解决办法:
在站点根目录下的web.config设置如下(主要参考添加项):
<system.webServer>
<modules>
<remove name="FormsAuthentication" />
<remove name="ApplicationInsightsWebTracking" />
<remove name="WebDAVModule" /> <!--添加项 -->
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
</modules>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<remove name="WebDAV" /> <!--添加项 -->
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
<validation validateIntegratedModeConfiguration="false" />
</system.webServer>
(End)
ASP.NET MVC- 在现有 MVC 项目添加 WebAPI【转】
1. 增加一个 WebApi Controller, VS 会自动添加相关的引用,主要有 System.Web.Http,System.Web.Http.WebHost,System.Net.Http
2. 在 App_Start 下创建 WebApiConfig.cs 并注册路由
using System;
using System.Collections.Generic;
using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web.Http; namespace Libaray.Web.App_Start { public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API 配置和服务 // Web API 路由 config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } } }
3. 在 Global.asax, Application_Start 下添加 WebAPI 配置
using Libaray.Web.App_Start;
using System;
using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; namespace Libaray.Web { public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configure(WebApiConfig.Register); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } } }
4. 在第一步添加的 WebApi 中填写相应代码,
using Libaray.Web.Models;
using System;
using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; namespace Libaray.Web.Controllers { [RoutePrefix("api/SystemUsers")] public class SystemUsersController : ApiController { [HttpGet, Route("GetUserList")] public List<UserModel> GetUserModels() { UserModelService UserBS = new UserModelService(); return UserBS.FindList(u => u.isActive == true); } [HttpGet, Route("GetUser")] public UserModel GetUserModel(int id = 0) { if(id != 0) { UserModelService UserBS = new UserModelService(); return UserBS.Find(u => u.Id == id); } else { return null; } } [HttpPost, Route("Login")] public bool Login(string loginId,string password) { UserModelService UserBS = new UserModelService(); return UserBS.ValidateLoginInfo(loginId, password); } } }
5. Run the application and call the API. Example: http://localhost:49919/api/SystemUsers/GetUserList
6. 一些疑问
做完以上配置后,运行网页,仍不能得到想要的结果,后查出因为我是在 Areas 里面建立了一个 API 的文件夹
只要将 View 文件夹和 APIAreaRegistration.cs 文件删除,问题就消失了,具体原因没有细究,应该还是路由的问题。
原文网址:
https://www.cnblogs.com/tuyile006/p/6151555.html
ASP.NET MVC4 WebAPI和Posting XML数据
前端正在使用jQuery:
$(document = function () { $("#buttonTestAPI").click(function () { var d = " <customer><customer_id>1234</customer_id></customer>"; $.ajax({ type: 'POST',contentType: "text/xml",url: "@Url.Content("~/api/Customer/")",data: d,success: function (result) { var str = result; $("#output").html(str); } }); }); });
我的控制器目前很简单 – 只是默认的后期操作 – 试图返回传递的内容:
public string Post(string value) { return value; }
但是,“值”反复为null.奇怪的是,当我将jQuery中的数据更改为这样的时候:
d = "<customer_id>1234</customer_id>";
然后我在控制器中获得“值”为1234.
我如何访问我的控制器中更复杂的xml字符串?
解决方法
public void PostRawXMLMessage(HttpRequestMessage request) { var xmlDoc = new XmlDocument(); xmlDoc.Load(request.Content.ReadAsstreamAsync().Result); }
您可以调试和检查正文,标题等,并会看到原始的XML发布.我使用fiddler的作曲家做一个HTTP POST,这样做很好.
asp.net-mvc – ASP.NET MVC 4 Web Api和REST经典服务之间的区别
但是普通Rest和ASP.Net MVC4 WebApi实际上有什么区别?
解决方法
REST是一个范例.
HTTP是遵循该范例的协议.
ASP.NET Web API允许开发人员编写可以通过HTTP访问并遵守REST范例的ASP.NET应用程序.虽然您可以在没有Web API的情况下创建REST API,但Web API提供了大量功能,这些功能将消除与在ASP.NET中创建真正RESTful API相关的许多痛苦.
apigee为REST API最佳实践提供了许多很好的资源.
今天关于asp.net-mvc-3 – WebAPI PUT / POST中的部分实体更新和asp.net updatepanel用法的讲解已经结束,谢谢您的阅读,如果想了解更多关于ASP.NET MVC WebAPI Put和Delete请求出现405(Method not allowed)错误、ASP.NET MVC- 在现有 MVC 项目添加 WebAPI【转】、ASP.NET MVC4 WebAPI和Posting XML数据、asp.net-mvc – ASP.NET MVC 4 Web Api和REST经典服务之间的区别的相关知识,请在本站搜索。
本文标签: