asp.net-web-api

Validating and passing controller-level parameters with ASP.NET MVC attribute routing

落花浮王杯 提交于 2020-01-24 22:31:57
问题 I have an ASP.NET controller where every single method will have a shared parameter. With attribute routing, I can add this parameter in the controller's route. However, I still need to add that parameter along with a validation attribute in every single method. Is there a way for me to do the validation in one place or avoid having to pass it in to every single method? This is the current working code: [ApiController] [Route("[controller]/{name}")] public class ExampleController :

Angular JS Not Sending Data To Web Api

谁说我不能喝 提交于 2020-01-24 21:08:55
问题 Angular Post method not sending data to web api Controller. $http call to web api but its null. 回答1: Define(Initialize) $FirstName , LastName , Email , Number as null first. $scope.FirstName = null; $scope.LastName = null; $scope.Email = null; $scope.Number = null; or You could just use $scope.user as $scope.user = { id : 0, FirstName : null, LastName : null, Email : null, Password : null }; And pass in ng-model as user.FirstName EDIT: Build $scope.user outside the CreateUser Function $scope

Why can't I make cross origin requests to an API hosted in IIS?

一世执手 提交于 2020-01-24 20:12:29
问题 I have an ASP.NET WebApi application running locally using IISExpress that allows me to accept requests from any domain. I am doing this using a DelegatingHandler similar to the one provided one this blob post. Locally this runs perfectly however after uploading to an Azure Website, I get the typical 'Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin.' under Chrome. I've tried to debug this by adding trace statements into the Handler with no output and it seems like

ASP.NET Web API PUT with catch-all route

ぃ、小莉子 提交于 2020-01-24 17:17:04
问题 With the new web api, is it possible to use a catch all route like routes.MapHttpRoute( name: "name", routeTemplate: "api/{*id}", defaults: new { controller = "mycontroller", id = RouteParameter.Optional} ); with the PUT method? Typically the PUT method is used like public HttpResponseMessage Put(string id, string body) { ... } where body is the body of the PUT request. However with a catch all route, this doesn't appear to work and I'm receiving the error { "ExceptionType":"System

How to modify ASP NET Web API Route matching to allow parameters with slashes inside?

余生颓废 提交于 2020-01-24 15:10:43
问题 We're using RavenDB on the backend and so all the DB keys are strings that contain forward slashes e.g. users/1 . AFAIK there's no way to use the default ASP NET Web API route matching engine to match a parameter like this, without using a catch-all which means the key must be the last thing on the URL. I tried adding a regex constraint of users/d+ but it didn't seem to make a difference, the route wasn't matched. What bits would I have to replace to do just enough custom route matching to

Is it possible to restrict access to REST API built using Web API 2?

旧巷老猫 提交于 2020-01-24 14:28:04
问题 I built a REST API using ASP.NET Web API 2, so I could deliver data from a backend database to my applications running on any platform (mobile, web, desktop etc) However up until now, I simply call the website with the controller I need data from and that's it, it sends back the JSON string in the response. But, the data is kind of special, and there is nothing to prevent another developer from simply calling the controllers and getting back the exact same data and building their own

.NET Web API + Session Timeout

冷暖自知 提交于 2020-01-24 12:59:30
问题 I am creating a web service with web api controller. I want to be able to create a session and check the status of the session. I have the following: Controller: public string Get(string user, string pass) { bool loginValue = false; loginValue = UserNamepassword(user, pass); if (loginValue == true) { HttpContext.Current.Session.Add("Username", user); //session["Username"] = user; //session.Add("Username", user); if ((string)HttpContext.Current.Session["Username"] != null) { HttpContext

How to support both kinds of URLs

核能气质少年 提交于 2020-01-24 12:15:08
问题 I'd like to be able to call GET by both: /api/Test/Test/1 /api/Test/Test?id=1 public class TestController : ApiController { [HttpGet] public string Test(int id) { return "Test"; } } How to configure the route? I have the default: config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); This makes "/api/Test/Test?id=1" work but not "/api/Test/Test/1" I tried to add the attribute

How to use InboundClaimTypeMap for claim mapping?

好久不见. 提交于 2020-01-24 12:12:47
问题 I have similar problem as here : https://github.com/IdentityServer/IdentityServer3.Samples/issues/9 But solution is not helpful for me. So lets explain in more details with pictures and code: I have this on client: No need to map because NameClaimType(RoleClaimType) and Claim in list of claims are same JwtSecurityTokenHandler.InboundClaimTypeMap.Clear(); On Api project I have: In this case (if I understand correctly), I have to to map, because NameClaimType & RoleClaimType are not same with

Call web api with basic authentication always get 401 unauthorized from IIS

喜夏-厌秋 提交于 2020-01-24 05:44:12
问题 Tonight i come search some help about how to call a web api hosted in IIS. Everything work well in local from visual studio to iis express. But strangely, after publish on my IIS server. I always get 401 unauthorized :'( Here is the code i use and the settings from my IIS server. I will be very grateful if somebody can help me. Thank you ** The controller and the function i try to call (with basic authentication attribute) ** [HttpGet] [ActionName("Get_UserID")] [IdentityBasicAuthentication]