asp.net-web-api2

Memory Cache in web api

旧街凉风 提交于 2019-11-30 09:50:55
I was looking for Caching in my web api where i can use output of one api method(that changes once in 12hrs) for subsequesnt calls and then i found this solution on SO,but i am having a difficulty in understanding and using the below code private IEnumerable<TEntity> GetFromCache<TEntity>(string key, Func<IEnumerable<TEntity>> valueFactory) where TEntity : class { ObjectCache cache = MemoryCache.Default; var newValue = new Lazy<IEnumerable<TEntity>>(valueFactory); CacheItemPolicy policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(30) }; //The line below returns

Where should I plug in my custom DefaultContractResolver JSON.NET?

最后都变了- 提交于 2019-11-30 09:37:23
问题 I have a WebAPI 2.0 project in ASP.Net in which I implemented a custom DefaultContractResolver so that I can control how my entities are serialized to JSON using JSON.Net; however I am unsure about how can I tell the framework to use my custom implementation. I'd also like to know if its possible to change the ContractResolver for a specific controller / action. Thanks! --- Edit 03/07/2014 I've already figured out how to use my custom implementation by creating a new ConfigSettings object and

Unable to change swagger ui path

醉酒当歌 提交于 2019-11-30 09:35:39
问题 I'm using Swagger / Swashbuckle version 5.6 to generate documentation for my ASP.Net Web API 2 project. By default API documentation is accessible at URL http://localhost:56081/swagger/ui/index But, I want it should be available at http://localhost:56081/apihelp/ I searched a lot, tried changing settings in the SwaggerConfig.cs file but nothing seems to make this work. So, is this even possible? if yes, can anyone help me with this ? 回答1: You can add the path to the call of EnableSwaggereUi ,

Timeout a Web Api request?

一笑奈何 提交于 2019-11-30 08:42:06
Like MVC WebApi runs on the asynchronous ASP.NET pipeline, meaning execution timeout is unsupported . In MVC I use the [AsyncTimeout] filter, WebApi doesn't have this. So how do I timeout a request in WebApi? Building on the suggestion by Mendhak, it is possible to do what you want, though not exactly the way you'd like to do it without jumping through quite a few hoops. Doing it without a filter might look something like this: public class ValuesController : ApiController { public async Task<HttpResponseMessage> Get( ) { var work = this.ActualWork( 5000 ); var timeout = this.Timeout( 2000 );

Unable to obtain configuration from well-known/openid-configuration

空扰寡人 提交于 2019-11-30 08:35:57
问题 I am using ASP.NET 5, In my solution I have Web API, Identity Server and Angular 2 project and I am authenticating Angular 2 client by using Identity Server, Angular 2 client consumes web api by passing token in http request and web api authenticate token and gives response, for this I have written a custom attribute which checks that user is authenticated or not When I consume API I am getting following exception and Web API returns 500 internal server error. System.InvalidOperationException

Enabling CORS through Web.config vs WebApiConfig and Controller attributes

人盡茶涼 提交于 2019-11-30 08:30:34
There seems to be two functionally different ways to enable cross-origin request sharing in Web API 2. One is to import System.Web.Http.Cors , decorate a controller with the EnableCors attribute and to write config.EnableCors() in the WebApiConfig: [EnableCors(origins: "http://111.111.111.111", headers: "*", methods: "*")] public class GenericController : ApiController { // etc. The other is to modify the Web.config : <system.webServer> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="http://111.111.111.111" /> <add name="Access-Control-Allow-Methods" value="*" />

Get Content-Disposition parameters

柔情痞子 提交于 2019-11-30 06:49:04
How do I get Content-Disposition parameters I returned from WebAPI controller using WebClient? WebApi Controller [Route("api/mycontroller/GetFile/{fileId}")] public HttpResponseMessage GetFile(int fileId) { try { var file = GetSomeFile(fileId) HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new StreamContent(new MemoryStream(file)); response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment"); response.Content.Headers.ContentDisposition.FileName = file.FileOriginalName; /********* Parameter *

Uniform, consistent error responses from ASP.Net Web API 2

北战南征 提交于 2019-11-30 06:44:08
I'm developing a Web API 2 application and I'm currently trying to format error resposnes in a uniform way (so that the consumer will also know what data object/structure they can inspect to get more info about the errors). This is what I've got so far: { "Errors": [ { "ErrorType":5003, "Message":"Error summary here", "DeveloperAction":"Some more detail for API consumers (in some cases)", "HelpUrl":"link to the docs etc." } ] } This works fine for exceptions thrown by the application itself (i.e inside controllers). However, if the user requests a bad URI (and gets a 404) or uses the wrong

How to add custom methods to ASP.NET WebAPI controller?

我的未来我决定 提交于 2019-11-30 06:32:39
问题 In ASP.NET MVC WebAPI project by default we have created following controller public class ValuesController : ApiController { // GET api/values public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; } // GET api/values/5 public string Get(int id) { return "value"; } // POST api/values public void Post([FromBody]string value) { } // PUT api/values/5 public void Put(int id, [FromBody]string value) { } // DELETE api/values/5 public void Delete(int id) { } } But is

ASP.NET Identity “Role-based” Claims

江枫思渺然 提交于 2019-11-30 06:27:16
I understand that I can use claims to make statements about a user: var claims = new List<Claim>(); claims.Add(new Claim(ClaimTypes.Name, "Peter")); claims.Add(new Claim(ClaimTypes.Email, "peter@domain.com")); But how should I store "role-based" claims? For example: The user is a super administrator. claims.Add(new Claim("IsSuperAdmin, "true")); The value parameter "true" feels completely redundant. How else can this statement be expressed using claims? This is already done for you by the framework. When user is logged in, all user roles are added as claims with claims type being ClaimTypes