asp.net-web-api2

Ambiguous Controller Names with Routing attributes: controllers with same name and different namespace for versioning

牧云@^-^@ 提交于 2019-12-18 02:44:32
问题 I am trying to add API versioning and my plan is to create a controller for each version in different namespace. My project structure looks like this (note: no separate area for each version) Controllers | |---Version0 | | | |----- ProjectController.cs | |----- HomeController.cs | |---Version1 | |----- ProjectController.cs |----- HomeController.cs I am using RoutingAttribute for the routes. So, ProjectController in Version0 has function with route as namespace MyProject.Controllers.Version0 {

How do you consume extra parameters in OAuth2 Token request within .net WebApi2 application

坚强是说给别人听的谎言 提交于 2019-12-17 22:35:04
问题 I have an api specific project in a large .net MVC 5 web solution. I am utilizing the WebApi2 templates out of the box to authenticate a user through the api. Using individual accounts to authenticate, the request body required to get an access token is: grant_type=password&username={someuser}&password={somepassword} This works as expected. However, I need to add a 3rd dimension to the scaffolded method "GrantResourceOwnerCredentials". In addition to checking the username/password, i need to

Where to store Bearer Token in MVC from Web API

痴心易碎 提交于 2019-12-17 21:52:32
问题 Scenario I have an ASP.NET Web API that uses the OAuth Password Flow to provide Bearer Tokens to gain access to its resources. I'm now in the process of making an MVC app that will need to use this API. The plan is to have the MVC controllers make calls to the API on behalf of the client browser. The ajax requests from the browser will hit the MVC controllers and then the API calls are made. Results are then fed back to the client as JSON and handles in java-script. The client should never

Swashbuckle Swagger - How to annotate content types?

风格不统一 提交于 2019-12-17 19:27:15
问题 How do I annotate my ASP.NET WebAPI actions so that the swagger metadata includes the content-types that my resources support? Specifically, I want the documentation to show that one of my resources can return the 'original' application/json and application/xml but also now returns a new format, application/vnd.blah+json or +xml . 回答1: What you need to do is this; Swagger spec: you need to add your response-type to the list of response-types for that operation "produces": [ "application/json"

WebApi 2 POST with single string parameter not working

空扰寡人 提交于 2019-12-17 18:16:44
问题 I have the following controller: public class ValuesController : ApiController { // POST api/values public IHttpActionResult Post(string filterName) { return new JsonResult<string>(filterName, new JsonSerializerSettings(), Encoding.UTF8, this); } } WebApi config config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional }); I use this js code to call the api $.ajax( { url: "/api/values/", type: "POST", dataType: 'json',

Global exception handling in OWIN middleware

风格不统一 提交于 2019-12-17 17:31:35
问题 I'm trying to create a unified error handling/reporting in ASP.NET Web API 2.1 Project built on top of OWIN middleware (IIS HOST using Owin.Host.SystemWeb). Currently I used a custom exception logger which inherits from System.Web.Http.ExceptionHandling.ExceptionLogger and uses NLog to log all exceptions as the code below: public class NLogExceptionLogger : ExceptionLogger { private static readonly Logger Nlog = LogManager.GetCurrentClassLogger(); public override void Log

HTTP 415 unsupported media type error when calling Web API 2 endpoint

此生再无相见时 提交于 2019-12-17 16:18:47
问题 I have an existing Web API 2 service and need to modify one of the methods to take a custom object as another parameter, currently the method has one parameter which is a simple string coming from the URL. After adding the custom object as a parameter I am now getting a 415 unsupported media type error when calling the service from a .NET windows app. Interestingly, I can successfully call this method using javascript and the jquery ajax method. The Web API 2 service method looks like this:

DB-First authentication confusion with ASP.NET Web API 2 + EF6

僤鯓⒐⒋嵵緔 提交于 2019-12-17 15:38:02
问题 I need to create a Web API C# application for an existing MySQL database. I've managed to use Entity Framework 6 to bind every database table to a RESTful API (that allows CRUD operations) . I want to implement a login/registration system (so that I can implement roles and permissions in the future, and restrict certain API requests) . The MySQL database I have to use has a table for users (called user ) that has the following self-explanatory columns: id email username password_hash It seems

ASP.NET Web API Authorization tokens expiring early

让人想犯罪 __ 提交于 2019-12-17 11:53:25
问题 I have implemented security for my web api (individual accounts) as discussed here. I have hosted the website on godaddy (shared hosting) and its working fine. When I ask for token by using url "domain.com/token", I get the token with expiration date of within 15 days. I have set this in "StartupAuth.cs" using AccessTokenExpireTimeSpan = TimeSpan.FromDays(15) e.g.: { "access_token":"qwertyuiop.....", "token_type":"bearer", "expires_in":1209599, "userName":"user@example.com", ".issued":"Wed,

ASP.NET Web API Authorization tokens expiring early

半城伤御伤魂 提交于 2019-12-17 11:52:41
问题 I have implemented security for my web api (individual accounts) as discussed here. I have hosted the website on godaddy (shared hosting) and its working fine. When I ask for token by using url "domain.com/token", I get the token with expiration date of within 15 days. I have set this in "StartupAuth.cs" using AccessTokenExpireTimeSpan = TimeSpan.FromDays(15) e.g.: { "access_token":"qwertyuiop.....", "token_type":"bearer", "expires_in":1209599, "userName":"user@example.com", ".issued":"Wed,