I have a controller
public class SimulatorController : ApiController
{
private SimulatorService _simulatorService;
public SimulatorController()
{
_simulatorService = new SimulatorService();
}
[HttpGet]
[Route("spiceusers")]
public async Task<IHttpActionResult> GetConsumerProductsAsync()
{
var consumerProductsList = await _simulatorService.GetConsumerProductsAsync();
return Ok(consumerProductsList);
}
}
And i have uri
http://comm-rpp-emulator.com/spiceusers/9656796/devicesfuse?includeComponents=true&groupByParentDevice=true&includeChildren=true&limit=50&page=1
I need that my method should processed
http://comm-rpp-emulator.com/spiceusers
and ignore othrer part of uri?
You can use the catch-all route parameters like {*segment} to capture the remaining portion of the URL path.
The assumption here is that attribute routing is already enabled.
public static class WebApiConfig {
public static void Register(HttpConfiguration config) {
// Web API routes
config.MapHttpAttributeRoutes();
// Other Web API configuration not shown.
}
}
The URL in the example posted can be matched to the action by using the catch-all route parameter which will capture the remaining portion of the URL path that matched the template
//GET spiceusers/{anything here}
[HttpGet]
[Route("~/spiceusers/{*url}")]
public async Task<IHttpActionResult> GetConsumerProductsAsync() { ... }
Now any calls to /spiceusers will map to the above action as intended.
Note that this also includes all sub calls below the spiceusers template, provided that is what was intended.
Note also that if this web api is being used along with the default MVC that the path my clash with the default routing. But given that wep api routes tend to be registered before MVC routes it may not be that much of an issue.
Related
I have tried all solutions found both here on Stackoverflow and elsewhere on the internet with regards to this error and still we are getting an issue.
So we have .NET API which has a POST method which then returns a CreatedAtRoute response (201). The problem is that when returning the CreatedAtRoute response we get the error "A route named 'X' could not be found in the route collection.", where X is the name of our route.
The Global.asax
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
GlobalConfiguration.Configuration.UseStructureMap<MasterRegistry>();
var allDirectRoutes = WebApiConfig.GlobalObservableDirectRouteProvider.DirectRoutes;
}
WebApi.config - We have the MapHttpAttributes declared before the default route.
public static class WebApiConfig
{
public static ObservableDirectRouteProvider GlobalObservableDirectRouteProvider = new ObservableDirectRouteProvider();
public static void Register(HttpConfiguration config)
{
config.Formatters.Clear();
config.Formatters.Add(new JsonMediaTypeFormatter());
// Web API routes
config.MapHttpAttributeRoutes(GlobalObservableDirectRouteProvider);
config.Routes.MapHttpRoute(
"DefaultApi",
"api/v1/{controller}/{id}",
new { id = RouteParameter.Optional }
);
}
}
Controller - GetCompoundById route
This is the route we want to build using the named route
[HttpGet]
[Route("{id:Guid}", Name = "GetCompoundById")]
[SwaggerResponse(HttpStatusCode.OK, Type = typeof(CompoundViewModel))]
public async Task<IHttpActionResult> Get(Guid id)
{
var serviceResult = await Task.FromResult(CompoundService.Get(id));
if (serviceResult == null)
{
return NotFound();
}
CompoundViewModel result =
new CompoundViewModel {Id = serviceResult.Id, Name = serviceResult.Name};
return Ok(result);
}
Controller - Return CreatedAtRoute in the POST action
This is where the error is thrown because the named route is not found.
return CreatedAtRoute("GetCompoundById", new {id = result.Id}, result);
Note: In the WebApi.config I have created an ObservableDirectRouteProvider which allows me to see the routes created on startup and I can see my named route exists in the collection.
If we are using route prefix at controller, we should define route name for all actions. Use this
[HttpGet]
[Route(Name = "GetCompounds")]
[SwaggerResponse(HttpStatusCode.OK, Type = typeof(IEnumerable<ApiModels.CompoundViewModel>))]
public async Task<IHttpActionResult> Get(int page = 0,int pageSize = CoreModels.Pagination.DefaultPageSize)
The issue was strangely nothing to do directly with the way we were using the named routes or configuring WebAPI routing (which explains why all other posts didn't help me fix it). We found that the issue was a side effect of how we were using StructureMap as our IoC container.
In the StructureMap registry we were calling
scanner.SingleImplementationsOfInterface();
which had the strange side-effect of causing the error. By performing a very long and tedious process of elimination I tracked it down to this exact line. Once remove the routing then again worked as expected. I can only presume that this causes some WebApi dependency to load the incorrect type into memory which cannot resolve the routing table.
I have a simple API with basic routing. It was setup using the default Visual Studio 2015 ASP.NET Core API template.
I have this controller and action:
[Route("api/[controller]")]
public class DocumentController : Controller
{
[HttpGet("info/{Id}")]
public async Task<Data> Get(string Id)
{
//Logic
}
}
So to reach this method, I must call GET /api/document/info/some-id-here.
Is it possible with .NET Core, inside that method, to retrieve as a string the complete route?
So I could do for example:
var myRoute = retrieveRoute();
// myRoute = "/api/document/info/some-id-here"
You can get the complete requested url using the Request option (HttpRequest) in .Net Core.
var route = Request.Path.Value;
Your final code.
[Route("api/[controller]")]
public class DocumentController : Controller
{
[HttpGet("info/{Id}")]
public async Task<Data> Get(string Id)
{
var route = Request.Path.Value;
}
}
Result route: "/api/document/info/some-id-here" //for example
You can also ask MVC to create a new route URL based on the current route values:
[Route("api/[controller]")]
public class DocumentController : Controller
{
[HttpGet("info/{Id}")]
public async Task<Data> Get(string Id)
{
//Logic
var myRoute = Url.RouteUrl(RouteData.Values);
}
}
Url.RouteUrl is a helper method that lets you build a route URL given any route values. RouteData.Values gives you the route values for the current request.
If you want the original route template on an API controller that was specified with any HttpMethod attribute then this will do it:
var routeAttribute = Url.ActionContext.ActionDescriptor.EndpointMetadata.First(d => d is HttpMethodAttribute);
var routeTemplate = ((HttpMethodAttribute)routeAttribute).Template;
If the original route attribute was: [HttpGet("Self/{id}")]
The routeTemplate value would be: "Self/{id}"
Url.ActionContext.ActionDescriptor.AttributeRouteInfo.Template
"v{version}/{cardId}/cardsTest" {$1}
I am trying to post to the following Web API:
http://localhost:8543/api/login/authenticate
LoginApi (Web API) is defined below:
[RoutePrefix("login")]
public class LoginApi : ApiController
{
[HttpPost]
[Route("authenticate")]
public string Authenticate(LoginViewModel loginViewModel)
{
return "Hello World";
}
}
WebApiConfig.cs:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
Here is the error I get:
Request URL:http://localhost:8543/api/login/authenticate
Request Method:POST
Status Code:404 Not Found
Remote Address:[::1]:8543
Your controller name "LoginApi" needs to end in "Controller" in order for the framework to find it. For example: "LoginController"
Here is a good article which explains routing in ASP.NET Web API: http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api
You are using login as your route prefix on your controller so trying to call
http://localhost:8543/api/login/authenticate
will not be found as this code
[RoutePrefix("login")]
public class LoginApi : ApiController
{
//eg:POST login/authenticate.
[HttpPost]
[Route("authenticate")]
public string Authenticate(LoginViewModel loginViewModel)
{
return "Hello World";
}
}
will only work for
http://localhost:8543/login/authenticate
You need to change your route prefix to
[RoutePrefix("api/login")]
public class LoginApi : ApiController
{
//eg:POST api/login/authenticate.
[HttpPost]
[Route("authenticate")]
public string Authenticate(LoginViewModel loginViewModel)
{
return "Hello World";
}
}
Notice you are using both attribute routing on the controller/action and convention routing with config.Routes.MapHttpRoute.
config.Routes.MapHttpRoute will map the routes as per your definition "api/{controller}/{id}".
While attribute routing, will map the routes based on how you've defined them: /login/authenticate.
Also, since you are using both attribute routing and convention routing, attribute routing takes presendence. I would stick to using one or the other. Having both adds a bit of confusion as to what route will be used to access an action method.
I am working on an asp.net 5 mvc api, and I am currently working on the Accounts Controller.
since I saw in many different places that there is a convention of using /api/Tokenrouting to a login in a web api. I would like to route to that specific method without the accounts prefix, I would prefer not using a different controller, and I would prefer using Attributes over routing in Startup.cs to avoid confusion in the future.
this is what I have currently
[Route("api/[controller]")]
public class AccountsController : Controller
{
[HttpPost("login")]
public async Task<JwtToken> Token([FromBody]Credentials credentials)
{
...
}
[HttpPost]
public async Task CreateUser([FromBody] userDto)
{
...
}
}
With attribute routing you can use a tilde (~) on the Action's route attribute to override the default route of the Controller if needed:
[Route("api/[controller]")]
public class AccountsController : Controller {
[HttpPost]
[Route("~/api/token")] //routes to `/api/token`
public async Task<JwtToken> Token([FromBody]Credentials credentials) {
...
}
[HttpPost]
[Route("users")] // routes to `/api/accounts/users`
public async Task CreateUser([FromBody] userDto) {
...
}
}
For ASP.NET Core it seems that the tilde ~ symbol (see accepted answer) is not needed anymore to override the controller's route prefix – instead, the following rule applies:
Route templates applied to an action that begin with a / don't get combined with route templates applied to the controller. This example matches a set of URL paths similar to the default route.
Here is an example:
[Route("foo")]
public class FooController : Controller
{
[Route("bar")] // combined with "foo" to map to route "/foo/bar"
public IActionResult Bar()
{
// ...
}
[Route("/hello/world")] // not combined; maps to route "/hello/world"
public IActionResult HelloWorld()
{
}
}
from https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing
[Route("[controller]/[action]")]
public class HomeController : Controller
{
[Route("~/")]
[Route("/Home")]
[Route("~/Home/Index")]
public IActionResult Index()
{
return ControllerContext.MyDisplayRouteInfo();
}
public IActionResult About()
{
return ControllerContext.MyDisplayRouteInfo();
}
}
In the preceding code, the Index method templates must prepend / or ~/ to the route templates. Route templates applied to an action that begin with / or ~/ don't get combined with route templates applied to the controller.
I've recently asked a few questions about the best way to create a web api which utilises the same url as my main mvc site. I deduced the best way was to make the necessary changes to my MVC site to include web api and the necessary routing.
I have mainly followed How to add Web API to an existing ASP.NET MVC 4 Web Application project? but I have run into problems. The code compiles fine and it is clearly looking for the route but I get the error:
No HTTP resource was found that matches the request URI 'http://localhost:2242/api/value'.
No type was found that matches the controller named 'value'.
My WebApiConfig:
class WebApiConfig
{
public static void Register(HttpConfiguration configuration)
{
configuration.Routes.MapHttpRoute("API Default", "api/{controller}/{id}",
new { id = RouteParameter.Optional });
}
}
my global.asax:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
Database.SetInitializer<ApplicationDbContext>(null);
}
}
my api controller:
public class ValuesController1 : ApiController
{
// GET api/<controller>
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/<controller>/5
public string Get(int id)
{
return "value";
}
// POST api/<controller>
public void Post([FromBody]string value)
{
}
// PUT api/<controller>/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/<controller>/5
public void Delete(int id)
{
}
}
Other posts have corroborated that this is a correct and working setup...I created a separate webapi project to compare and this is all correct routing wise apparently. It would be far preferable to build this into my MVC website, does anyone have any ideas? This poster No type was found that matches controller had the same problem and the solution he found was to copy everything into a new project....that really isn't something I want to do/see why I should need to do.
I think it is because of your Controller's name : ValuesController1
A controller has to be suffixed by "Controller", the 1 may be the cause of your issue.
The name of the controller ValuesController1 doesn't match convention - in order for the default route to match /api/value based on the default convention set in your call to configuration.Routes.MapHttpRoute(...), the controller should be called ValueController:
public class ValueController : ApiController
{
public IEnumerable<string> Get()
// ...
However, if you intend to deviate from the configured convention, you can apply RouteAttribute and RoutePrefixAttribute in conjunction with the Http* verb attributes to customise controller and method routes, e.g.
[RoutePrefix("api/Foo")]
public class ValuesController : ApiController
{
// get api/Foo/value
[HttpGet]
[Route("value")]
public IEnumerable<string> NameDoesntMatter()
{
return new string[] { "value1", "value2" };
}
// get api/Foo/value/123
[HttpGet]
[Route("value/{id}")]
public string AnotherRandomName(int id)
{
return "value";
}
Before using the RouteAttribute you will need to add the following to your WebApiConfig.Register(HttpConfiguration config):
config.MapHttpAttributeRoutes();
Even with the routing attributes, note however that the controller class name still needs to end with the suffix Controller, i.e. cannot end in the suffix 1. It is surprisingly difficult to alter this convention.