asp.net-web-api2

webapi and unhandled exception hook per appdomain

不问归期 提交于 2019-12-07 03:30:31
Having a WebApi 2.2 application - is it possible to make use of AppDomain's UnhandledException hook? I have a code similar to this: [assembly: OwinStartup("DevConfiguration", typeof(DevStartup))] namespace WebApi.Module { public class DevStartup { private const string ErrorEventSourceName = "WebApiHost"; private const int ErrorEventId = 600; [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlAppDomain)] public void Configuration(IAppBuilder app) { AppDomain.CurrentDomain.UnhandledException += AppDomain_UnhandledException; ... throw new Exception("some exception); }

Return a String in Web API 2

六月ゝ 毕业季﹏ 提交于 2019-12-07 03:27:02
问题 As simple as it sounds, I haven't found any documentation on this and perhaps I'm wording it wrong so if so, some good documentation on this would be appreciated. I simply want to return a string or a model of type string. return "string here"; I get this error every time: Severity Code Description Project File Line Error CS0029 Cannot implicitly convert type 'string' to 'System.Web.Http.IHttpActionResult' TaskManagement C:\dev\TaskManagement\TaskManagement\Controllers\JobsController.cs 157 I

Updating Roles when granting Refresh Token in Web Api 2

守給你的承諾、 提交于 2019-12-07 03:07:53
问题 I have developed an authentication mechanism in Asp.Net Web Api 2 with the feature for granting refresh tokens, based on the tutorial on Taiseer's blog. Here is my question. Assume the following scenario: A user logs in using password and get a refresh token and an access token. The access token in fact includes what roles he is in (hence his authorities within the app). In the mean time the system admin will change this person's roles, so once his access token expires and he wants to use the

Handling errors/exceptions in a mediator pipeline using CQRS?

丶灬走出姿态 提交于 2019-12-07 02:26:54
问题 I'm trying to follow this post by Jimmy Bogard to implement a mediator pipeline so I can use pre/post request handlers to do some work. From the comments on that article I come to this github gist. I don't quite understand how to hook all of this up yet, so here is my first go. FYI - I'm using Autofac for DI and Web Api 2. Following CQRS, here is a query. public class GetAccountRequest : IAsyncRequest<GetAccountResponse> { public int Id { get; set; } } //try using fluent validation public

Unit testing ASP.NET Web API 2 Controller which returns custom result

╄→гoц情女王★ 提交于 2019-12-07 02:24:20
问题 I have a Web API 2 controller which has an action method like this: public async Task<IHttpActionResult> Foo(int id) { var foo = await _repository.GetFooAsync(id); return foo == null ? (IHttpActionResult)NotFound() : new CssResult(foo.Css); } Where CssResult is defined as: public class CssResult : IHttpActionResult { private readonly string _content; public CssResult(string content) { content.ShouldNotBe(null); _content = content; } public Task<HttpResponseMessage> ExecuteAsync

How to manage cache in ASP.NET WebApi2?

好久不见. 提交于 2019-12-07 02:21:37
问题 I have implemented REST service using WebAPI2, service implemeted to manage different sessions which are created and joined by different clients which are accessing service. Session contains information about access of application functionality and information of participants which have joined same session. Each client get session information and access list from server for synchronization purpose on every second. According to access changed, client functionality will changed(Enable/Disable).

.Net Core WebAPI , Unable to post data with POSTMAN , error - 415 Unsupported MediaType

杀马特。学长 韩版系。学妹 提交于 2019-12-06 19:01:09
问题 I am testing my first .net Core WebAPI with Postman unknown media type error is occurring. What am I missing? This is my posting object public class Country { [Key] public int CountryID { get; set; } public string CountryName { get; set; } public string CountryShortName { get; set; } public string Description { get; set; } } This is the webapi controller [HttpPost] public async Task<IActionResult> PostCountry([FromBody] Country country) { if (!ModelState.IsValid) { return BadRequest

Set default for DisplayFormatAttribute.ConvertEmptyStringToNull to false

谁说我不能喝 提交于 2019-12-06 18:28:18
问题 I just converted a bunch of web services to Web API2. Now my C# code blows up when the browser sends an empty string and it enters my code converted to null. I have researched global solutions and none that I have found work for me. I can of course set it manually for every string in all my Web API models, but I have scores of models so would prefer a global solution. Been here: string.empty converted to null when passing JSON object to MVC Controller and other pages and attempted to

ExceptionFilter stack trace in synchronous action scenario

孤街浪徒 提交于 2019-12-06 16:36:06
I have a simple synchronous action in a Controller that throws an exception like so : [RoutePrefix("")] public class MyController : ApiController { [Route("")] public HttpResponseMessage Get() { throw new Exception("whatever"); return Request.CreateResponse(HttpStatusCode.OK, "response"); } } I also have an ExceptionFilterAttribute to get a hold of exceptions that occur in the application public class MyExceptionFilterAttribute : ExceptionFilterAttribute { public override void OnException(HttpActionExecutedContext actionContext) { var ex = actionContext.Exception; // Log ex, etc. } }

Get the api controllers constructor value within an AuthorizeFilter

拜拜、爱过 提交于 2019-12-06 14:46:50
问题 When the user is authenticated I want to prevent that he updates/deletes/reads data created from other accounts... by telling him you do not have the permission 403! What is the best way to get an instance of the ISchoolyearService to invoke its HasUserPermission() method? I know I could new up the SchoolyearService here but that would defeat the reason using an IoContainer at all in my app. public class UserActionsSchoolyearAuthorizationFilter : AuthorizationFilterAttribute { public override