Tag: asp.net web api

将多种内容类型发布到web api

我有一个web api,我想发布一个图像文件+一些数据,以便在服务器收到它时正确处理它。 调用代码看起来像这样: using(var client = new HttpClient()) using(var content = new MultipartFormDataContent()) { client.BaseAddress = new Uri(“http://localhost:8080/”); var fileContent = new ByteArrayContent(File.ReadAllBytes(fileName)); fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue(“attachment”) { FileName = “foo.jpg” }; content.Add(fileContent); FeedItemParams parameters = new FeedItemParams() { Id = “1234”, comment = “Some comment about this or that.” }; content.Add(new ObjectContent(parameters, new JsonMediaTypeFormatter()), […]

如何在asp.net Core中使用JWT

我按照JWT身份validation为Asp.Net Web Api回答将JWT添加到我的asp.net核心项目但是.net core中不支持System.Web.Http.Filters ? 任何更好的保护Web API的解决方案???

WebAPI OAuth注销 – 如何删除令牌Cookie?

我有一个OAAP登录的WebAPI配置如下: app.UseOpenIdConnectAuthentication( new OpenIdConnectAuthenticationOptions { ClientId = clientId, Authority = authority, PostLogoutRedirectUri = “https://www.microsoft.com/”, Notifications = new OpenIdConnectAuthenticationNotifications { AuthenticationFailed = context => { context.HandleResponse(); context.Response.Redirect(“/Error?message=” + context.Exception.Message); return Task.FromResult(0); } } }); 和使用登录强制执行所有控制器 config.Filters.Add(new System.Web.Http.AuthorizeAttribute()); 我现在想要添加一个名为LogoutController的ApiController(猜猜它是做什么的)。 我发现我可以使用从MVC注销 System.Web.Security.FormsAuthentication.SignOut(); 但我没有这样从WebAPI注销。 我还没有找到任何有关如何从WebAPI注销的信息。 但是我发现注销过程中可能存在错误,cookie被保留并且必须手动删除 ,但是,代码再次是MVC,似乎我无法将HttpCookie放入我的HttpResponseMessage对象中: [HttpGet] public HttpResponseMessage Logout() { FormsAuthentication.SignOut(); // clear authentication cookie HttpCookie cookie1 […]

ASP.NET Web API的dependency injection

我试图在我的ASP.NET Web API控制器中使用DI。 显然有一些实现。 我只是想知道哪些(Castle,Ninject,unity等)与ASP.NET Web API结合起来最容易配置/维护? 我收到此错误: DI.Controllers.ImportJsonController’没有默认构造函数。 如果我添加一个空构造函数,则忽略带有IFilter的构造函数。 这是我的控制器: public class ImportJsonController : ApiController { private readonly IFilter _filter; public ImportJsonController(IFilter filter) { _filter = filter; } public HttpResponseMessage Post([FromBody]dynamic value) { //do something return response; } }

使用ASP.Net webapi的Angular.js $资源?

我有一个简单的ASP.Net WebAPI服务,它在POST / PUT上分配一个静态变量并返回GET上的值: private static State Repo = null; public State Get() { return Repo; } public void Post(State value) { Repo = value; } public void Put(State value) { Repo = value; } 我有一个像这样定义的角度资源: var stateService = angular.module(‘StateService’, [‘ngResource’]); stateService.factory(‘State’, function ($resource) { return $resource(‘http://localhost:8080/API/State’); }); 当我尝试这样做时: State.get(function (state) { $scope.data = state.data […]

如果没有使用“占位符”而没有其他任何事情可以安全循环,我怎么能安全地循环?

为了调用我的Web API方法,直到不再返回数据(我正在批量获取它,为了保持每个结果集很小,由于客户端的98-lb弱点角色(Windows CE手持设备)),我正在使用此代码: while (moreRecordsExist) { redemptionsList.redemptions.Clear(); string uri = String.Format(“http://platypus:28642/api/Redemptions/{0}/{1}”, lastIdFetched, RECORDS_TO_FETCH); var webRequest = (HttpWebRequest)WebRequest.Create(uri); webRequest.Method = “GET”; using (var webResponse = (HttpWebResponse)webRequest.GetResponse()) { if (webResponse.StatusCode == HttpStatusCode.OK) { var reader = new StreamReader(webResponse.GetResponseStream()); string s = reader.ReadToEnd(); var arr = JsonConvert.DeserializeObject(s); if (null == arr) break; foreach (JObject obj in arr) { […]

尝试使用HttpClient上传文件时,Web Api的RequestEntityTooLarge响应

我正在尝试使用Asp.Net Web Api创建一个通过Post上传文件的Web服务。 这些分别是Client和Web Api的实现: 客户: using (var client = new HttpClient()) { client.BaseAddress = new Uri(“https://127.0.0.1:44444/”); using (var content = new MultipartFormDataContent(“Upload—-” + DateTime.Now.ToString(CultureInfo.InvariantCulture))) { content.Add(new ByteArrayContent(File.ReadAllBytes(filepath))); content.Headers.ContentType = new MediaTypeHeaderValue(“application/octet-stream”); var response = await client.PostAsync(“api/Synchronization”, content); if (response.IsSuccessStatusCode) eventLog1.WriteEntry(“Synchronization has been successful”, EventLogEntryType.Information); else eventLog1.WriteEntry(response.StatusCode + “:” + response.ReasonPhrase, EventLogEntryType.Error); } } 服务器: […]

如何向我未存储在数据库中的Web API响应中添加属性?

我正在使用Entity Framework 6.0构建C#Web API。 我有最简单的用户类,有3个属性,我坚持将SQL存入用户表,其中有3个相应的列,其中UserID是其主键。 public partial class User { public string UserID {get; set;} public string FirstName {get; set;} public string LastName {get; set;} } 我想在Web API中添加两个仅用于输出的属性,我不想在我的数据库中存储这些属性。 我使用这些属性与消费客户端“状态”和“消息”信息进行通信,这些信息不属于用户类。 状态=确定|错误|警告。 消息将是Web API需要与调用客户端通信的任何消息。 我的问题是:在发送回Web API的响应而不修改SQL上的基础用户表时,动态添加这两个属性的最简单方法是什么? 我知道我可以将这两个作为虚拟列添加到用户表中。 当我不需要它时,我不想在SQL端带来那些开销。

为什么添加额外的标头使AJAX调用失败

AJAX电话: $.ajax({ url: “http://myserver2:296/api/Demo/HelloWorld”, type: “GET”, dataType: ‘JSONP’, jsonp: “callback”, headers: { ‘API_KEY’: ‘mykey09090’ }, success: function (result) { console.log(result); }, error: ajaxFailed }); function ajaxFailed(xmlRequest) { alert(xmlRequest.status + ‘ \n\r ‘ + xmlRequest.statusText + ‘\n\r’ + xmlRequest.responseText); } 我收到以下错误: Failed to load resource: the server responded with a status of 403 (Forbidden) 。 但是当我使用Postman时,我只需要添加带有http://myserver2:296/api/Demo/HelloWorld […]

如何在Asp WebAPI应用程序中实现一个非常长时间运行的后台任务

我正在编写一个WebAPI应用程序来执行许多长时间运行的后台处理任务。 我注意到,即使我在IIS中禁用了池回收,我的测试数据加载任务也被System.Threading.ThreadAbortException杀死了。 我知道,有一种方法可以在Web应用程序中运行一个长时间运行(几个小时)的后台任务,但是,我只是不知道这是怎么做到的 – 要么是一些特定的配置设置,一种编码技术,一个Wen.config参数等等……请问有人可以分享一些实用的方法吗?