asp.net-web-api-routing

How to force web API to recognise querystring parameter

六月ゝ 毕业季﹏ 提交于 2019-12-04 00:09:56
ASP.NET MVC4 Web API v1 controller is defined below. It should accept 1 or 2 query string parametrs. However ko parameter is always null if method is called. Request is below. How to fix so that klient or namepart parameter can passed in query string ? Web API v1 controller: namespace MyApp.Controllers { public class CustomersSearchViewModel { public string Klient { get; set; } public string Namepart { get; set; } } [Authorize] public class CustomersController : ApiController { public HttpResponseMessage Get(CustomersSearchViewModel ko) { // why ko is null ? var res = GetCustomers(ko.Klient,ko

Web Api Controller in other project, route attribute not working

旧街凉风 提交于 2019-12-03 23:12:13
I have a solution with two projects. One Web Api bootstap project and the other is a class library. The class library contains a ApiController with attribute routing. I add a reference from web api project to the class library and expect this to just work. The routing in the web api is configured: config.MapHttpAttributeRoutes(); The controller is simple and looks like: public class AlertApiController:ApiController { [Route("alert")] [HttpGet] public HttpResponseMessage GetAlert() { return Request.CreateResponse<string>(HttpStatusCode.OK, "alert"); } } But I get a 404 when going to the url "

ASP.NET Web Api 2 - Subdomain Attribute Routing

爱⌒轻易说出口 提交于 2019-12-03 22:07:14
I've been using AttributeRouting for quite some time in my MVC application. However, one thing it always lacked is subdomain routing in Web Api (among other features in that library that work with MVC but not Web Api). Now I just read about the new improvements in Web Api regarding Attribute Routing and that it's now included with Web Api out of the box. However, I see no mention of subdomain routing. Is it supported in Web Api 2? If not, how can I get subdomain routing in my Web Api so that I can hit the ApiController using http://api.mydomain.com/cars/1 ? Routing is normally used for the

Routing with action after id parameter in Web API

冷暖自知 提交于 2019-12-03 20:35:40
In web api the default route is: /api/locations/123?days=5 config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); But what if I wanted the route to look like this /api/locations/123/events?days=5 while still being able to hit the LocationsController with a route like this /api/locations/123?state=md Controller: public class LocationsController : ApiController { // GET api/locations/123/events?days=5 public IEnumerable<Event> GetEventsByDays(int idLocation, int days) { // do stuff } // GET api/locations/123?state

.Net WebApi OData Actions that return an Queryable

痴心易碎 提交于 2019-12-03 17:50:36
问题 I want to achieve something close to the RateProduct action described in: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-actions In that tutorial it is defined as: [HttpPost] public int RateProduct([FromODataUri] int key, ODataActionParameters parameters) { // ... } ODataModelBuilder modelBuilder = new ODataConventionModelBuilder(); modelBuilder.EntitySet<Product>("Products"); // New Code ActionConfiguration rateProduct = modelBuilder.Entity<Product>().Action(

MVC API Routing When Multiple Get Actions Are Present

我与影子孤独终老i 提交于 2019-12-03 17:04:52
There seems to be a thousand people asking the same question on stack overflow, but there doesn't seem to be a single solution to this problem. I am going to ask it again... I have an API controller that has the following actions: // GET api/Exploitation public HttpResponseMessage Get() { var items = _exploitationRepository.FindAll(); var mappedItems = Mapper.Map<IEnumerable<Exploitation>, IEnumerable<ExploitationView>>(items); var response = Request.CreateResponse<IEnumerable<ExploitationView>>(HttpStatusCode.OK, mappedItems); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { }

Routing an Angular 2 app with Web API

只愿长相守 提交于 2019-12-03 16:36:53
I have an Angular 2 application created with Angular CLI. This has to call a .NET 4.6 Web API. The route setup of this is driving me nuts. For Angular, the default folder for output is /dist . Angular CLI does all the minification and tree-shaking you can dream of and then outputs both its JavaScript files and index.html to that folder. So if I run index.html , those files are retrieved from the same folder. Everything works well, because index.html contains a tag like this one: <base href="/"> The Web API project has the expected structure, with the Angular app a parasite in its root. So it

How can I generate a WebApi2 URL without specifying a Name on the Route attribute with AttributeRouting?

帅比萌擦擦* 提交于 2019-12-03 16:16:31
问题 I've configured my ASP.NET MVC5 application to use AttributeRouting for WebApi: public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.MapHttpAttributeRoutes(); } } I have an ApiController as follows: [RoutePrefix("api/v1/subjects")] public class SubjectsController : ApiController { [Route("search")] [HttpPost] public SearchResultsViewModel Search(SearchCriteriaViewModel criteria) { //... } } I would like to generate a URL to my WebApi controller

MVC Routes within WebAPI controller

百般思念 提交于 2019-12-03 15:59:39
Quick question regarding routes within MVC and WebAPI. I have added a route to route config.cs: routes.MapRoute( name: "ConfirmEmail", url: "ConfirmEmail/{userid}", defaults: new { controller = "Email", action = "ConfirmEmail" } ); This is registered in the global.asax as per normal: RouteConfig.RegisterRoutes(RouteTable.Routes); I am trying to generate a URL for use within an email which is sent as part of a function call within a WebAPI controller function. I am using the UrlHelper.Link function to attempt to generate a URL, however I receive an error saying the route cannot be found by name

Multiple controllers with same URL routes but different HTTP methods

旧街凉风 提交于 2019-12-03 14:11:58
I've got a following two controllers: [RoutePrefix("/some-resources") class CreationController : ApiController { [HttpPost, Route] public ... CreateResource(CreateData input) { // ... } } [RoutePrefix("/some-resources") class DisplayController : ApiController { [HttpGet, Route] public ... ListAllResources() { // ... } [HttpGet, Route("{publicKey:guid}"] public ... ShowSingleResource(Guid publicKey) { // ... } } All three actions got in fact three different routes: GET /some-resources POST /some-resources GET /some-resources/aaaaa-bbb-ccc-dddd If I put them into single controller everything