Web Api Ignoring Route Attribute - c#

I am working on a Web API project and when I run it and try to use the route for a controller, it doesn't work and instead, it throws me a 404 error. Why is the Web API ignoring the route attribute?
I'll leave the code below:
[ApiController]
[Route("api/[controller]")]
public class Controller3 : ControllerBase
{
private readonly IServiceContract3 _interest;
public Controller3(IServiceContract3 interest)
{
_interest = interest;
}
[HttpGet]
[Route("[action]")]
[Route("api/Interest/GetInterests")]
public IEnumerable<Interest> GetEmployees()
{
return _interest.GetInterests();
}
[HttpPost]
[Route("[action]")]
[Route("api/Interest/AddInterest")]
public IActionResult AddInterest(Interest interest)
{
_interest.AddInterest(interest);
return Ok();
}
[HttpPost]
[Route("[action]")]
[Route("api/Interest/UpdateInterest")]
public IActionResult UpdateInterest(Interest interest)
{
_interest.UpdateInterest(interest);
return Ok();
}
[HttpDelete]
[Route("[action]")]
[Route("api/Interest/DeleteInterest")]
public IActionResult DeleteInterest(int id)
{
var existingInterest = _interest.GetInterest(id);
if (existingInterest != null)
{
_interest.DeleteInterest(existingInterest.Id);
return Ok();
}
return NotFound($"Employee Not Found with ID : {existingInterest.Id}");
}
[HttpGet]
[Route("GetInterest")]
public Interest GetInterest(int id)
{
return _interest.GetInterest(id);
}
}
And for my Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddDbContextPool<DatabaseContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DB")));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, DatabaseContext context)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
context.Database.Migrate();
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
How can I fix the routing? Every time I try to do it in my browser like https://localhost:44316/api/Interest/GetInterests I get a 404 error instead. Why is this happening?

Typically controllers for a webapi are defined as so:
[ApiController]
[Route("api/[controller]")]
public sealed class InterestController : ControllerBase
{
private readonly IServiceContract3 _interest;
public InterestController(IServiceContract3 interest)
{
_interest = interest;
}
[HttpGet, Route("GetInterests")]
public IActionResult GetEmployees()
{
// this is located at: GET api/Interest/GetInterests
return Ok(_interest.GetInterests());
}
[HttpPost, Route("AddInterest")]
public IActionResult AddInterest([FromBody] Interest interest)
{
// this is located at: POST api/Interest/AddInterest
}
[HttpPost, Route("UpdateInterest")]
public IActionResult UpdateInterest([FromBody] Interest interest)
{
// this is located at: POST api/Interest/UpdateInterest
}
[HttpDelete, Route("DeleteInterest/{id}")]
public IActionResult DeleteInterest([FromRoute] int id)
{
// this is located at: DELETE api/Interest/DeleteInterest/{id}
}
[HttpGet, Route("GetInterest/{id}")]
public IActionResult GetInterest([FromRoute] int id)
{
// this is located at: GET api/Interest/GetInterest/{id}
return Ok(_interest.GetInterest(id));
}
}
First, you want to name your controller a relevant name that will carry throughout the rest of your controller. So, this this case, I changed Controller3 to InterestController. Since the route of the controller is "/api/[Controller]", it will translate to "api/Interest".
Next, remove the [Action] attributes. You do not need them in this scenario.
Then, make sure your routes are correct. If you are passing an ID, define your ID in the route using {id} and setting the [FromRoute] attribute for clarity. Also, use [FromBody] to define parameters that will be coming from the body. A lot of these are "default" (meaning you don't need to add them), but it helps with clarity.
Finally, if you are using the IActionResult pattern, then stick with that through your controller. Don't mix/match return types across endpoints because it's confusing for maintenance.
One final thing:
Your controller endpoint names are slightly redundant. For example, you could do this:
[HttpGet, Route("")]
public IActionResult GetEmployees()
{
// this is located at: GET api/Interest
return Ok(_interest.GetInterests());
}
[HttpPut, Route("")]
public IActionResult AddInterest([FromBody] Interest interest)
{
// this is located at: PUT api/Interest/
}
[HttpPost, Route("")]
public IActionResult UpdateInterest([FromBody] Interest interest)
{
// this is located at: POST api/Interest/
}
[HttpDelete, Route("{id}")]
public IActionResult DeleteInterest([FromRoute] int id)
{
// this is located at: DELETE api/Interest/{id}
}
[HttpGet, Route("{id}")]
public IActionResult GetInterest([FromRoute] int id)
{
// this is located at: GET api/Interest/{id}
return Ok(_interest.GetInterest(id));
}
i.e.: Use the "HTTP Verb" to your advantage to segregate actions.

you have to fix your route by making api a root. Replace "api" by "~/api". IMHO you should remove [action] from actions and add it to a controller. Also fix a controller name
[ApiController]
[Route("~/api/[controller]/[action]")]
public class InterestController : ControllerBase
[HttpGet("~/api/Interest/GetInterests")]
public IEnumerable<Interest> GetEmployees()
....
[HttpPost("~/api/Interest/AddInterest")]
public IActionResult AddInterest(Interest interest)
...
[HttpPost("~/api/Interest/UpdateInterest")]
public IActionResult UpdateInterest(Interest interest)
...
[HttpDelete("~/api/Interest/DeleteInterest/{id}")]
public IActionResult DeleteInterest(int id)
....
[HttpGet("~/api/Interest/GetInterest/{id}")]
public Interest GetInterest(int id)

Related

Is it possible to use API versioning for specific methods within a controller in ASP.NET Core?

I'm using the API versioning methods provided by Microsoft.AspNetCore.Mvc.Versioning (v4.1).
So in the startup class I have the following:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddMvc(options => options.Filters.Add<ExceptionFilter>());
services.AddApiVersioning(options =>
{
options.ApiVersionReader = new MyCustomVersionReader(configuration);
options.AssumeDefaultVersionWhenUnspecified = false;
options.ReportApiVersions = true;
options.ApiVersionSelector = new CurrentImplementationApiVersionSelector(options);
});
}
In the controller I can then add annotation [ApiVersion("1.0")] to the controller or individual methods.
[HttpGet]
[ApiVersion("1.0")]
public async Task<IActionResult> Get() {...}
Using this, all calls to the api have to have version info in the request header.
However, is it possible to have a specific method within the controller not require a version?
e.g.
[HttpGet]
[ApiVersion("1.0")]
public async Task<IActionResult> Method1() {...} //requires version in header
[HttpGet]
public async Task<IActionResult> Method2() {...} //does not require version in header
When I try the above (omitting the ApiVersion annotation for Method2) I still get the following error:
'An API version is required, but was not specified.'
Thank you for helping!
I think you can use [ApiVersionNeutral] attribute.You can see my demo below.
First in your startup add:
options.Conventions.Controller<HomeV1Controller>().HasApiVersion(new Microsoft.AspNetCore.Mvc.ApiVersion(1, 0));
Then in your HomeV1Controller:
[Route("api/[controller]")]
[ApiController]
public class HomeV1Controller : ControllerBase
{
[HttpGet("get")]
public string Get() => "Ok1";
[HttpGet("getv2")]
[ApiVersionNeutral]
public string GetV2() => "Ok2";
}
Test result:

How could I define the route for RestAPI on startup in ASP.NET Core?

I know how to define the route using attribute, eg:
[Route("api/v1/[controller]")]
[ApiController]
public class OTGController
{
[HttpGet("UpdateData")]
public void UpdateData()
{
// to do...
}
}
But this is not very easy to use, I need to config it any time when I add a new controller.
First, the route for the controller is always: "api/v1/[controller]". I don't want to configure it in each controller.
Second, the route for UpdateData is "UpdateData". They are the same. I want to define it like:
[HttpGet]
public void UpdateData()
{
// to do...
}
If I leave the route with empty, I want it to use the method name UpdateData as the route. This is different from the default behavior.
If it is not empty, I want it to use the route defined.
I want my final code like:
[ApiController]
public class OTGController
{
[HttpGet]
public void UpdateData()
{
// to do...
}
}
No route is defined in this class and the route should be generated successfully according to the rule I configure on startup.
How?
Solution 1:
You can override the default EndpointMiddleware middleware. Update the function Configure(IApplicationBuilder app, IWebHostEnvironment env) on Startup class.
...
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
"Default",
"api/v1/{controller}/{action=UpdateData}"
);
});
...
Warning: You will need to remove the ApiControllerAttribute to your Api controller(s). Action methods on controllers annotated with ApiControllerAttribute must be attribute routed.
Solution 2:
You can always create an API base controller class.
[ApiController]
[Route("api/v1/[controller]")]
public class ApiControllerBase : ControllerBase
{
[HttpGet("")]
public virtual IActionResult UpdateData()
{
return NoContent();
}
}
public class OTGController : ApiControllerBase
{
public override IActionResult UpdateData()
{
return Ok("Updated!");
}
}
Solution 3:
EndpointMiddleware configuration:
...
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
"Default",
"api/v1/{controller}/{action=UpdateData}"
);
endpoints.MapControllers();
});
...
Controllers:
[ApiController]
[Route("api/v1/[controller]")]
public class ApiControllerBase : ControllerBase
{
}
public class OTGController : ApiControllerBase
{
public IActionResult UpdateData()
{
return Ok("Updated!");
}
}

ASP.NET Web API Attribute Routing To Wrong Action

I have applied attribute routing on my controller and it'srouting to wrong action. I don't know where I am getting it wrong.
Here is my controller:
using System.Collections.Generic;
using System.Web.Http;
using System.Web.Http.Description;
using System.Linq;
using System;
namespace Iboo.API.Controllers
{
public class ClientsController : ApiController
{
private readonly IClientRepository _repository;
public ClientsController(IClientRepository repository)
{
_repository = repository;
}
// GET: api/Clients
[Route("api/v1/clients")]
public IEnumerable<Client> Get()
{
//code
}
// GET: api/Clients/5
[HttpGet]
[ResponseType(typeof(Client))]
[Route("api/v1/clients/get/{id}")]
public IHttpActionResult GetClientById(int id)
{
//code
}
// GET: api/Clients/5
[HttpGet]
[ResponseType(typeof(string))]
[Route("api/v1/clients/{id}/emailid")]
public IHttpActionResult GetClientEmailId(int id)
{
//code
}
}
}
I am specifically interested in the GetClientEmailId method. Below is my WebApiConfig
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
var container = new UnityContainer();
container.RegisterType<IClientRepository, ClientRepository>(new
HierarchicalLifetimeManager());
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/v1/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
My Global.asax.cs is as follows
public class WebApiApplication : 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);
}
}
In the browser If I type http://localhost:54919/api/v1/clients/?id=1/getemailid it's taking me to http://localhost:54919/api/v1/clients which is not what I want.
If I try http://localhost:54919/api/v1/clients/1/getemailid I am getting a 404 error.
I am not sure as to what I'm getting wrong.
You are calling the wrong URLs according to routes on the actions. you get 404 because the URL you call does not match to any of the route templates you have on your actions
[RoutePrefix("api/v1/clients")]
public class ClientsController : ApiController {
//...other code removed for brevity
[HttpGet]
[Route("")] //Matches GET api/v1/Clients
public IHttpActionResult Get() {
//code
}
[HttpGet]
[ResponseType(typeof(Client))]
[Route("{id:int}")] //Matches GET api/v1/Clients/5
public IHttpActionResult GetClientById(int id) {
//code
}
[HttpGet]
[ResponseType(typeof(string))]
[Route("{id:int}/emailid")] //Matches GET api/v1/Clients/5/emailid
public IHttpActionResult GetClientEmailId(int id) {
//code
}
}
Take note of the expected URLs in the comments
You should also read up on Attribute Routing in ASP.NET Web API 2 to get a better understanding of how to do attribute-routing.
You can try using the route prefix on the controller.
[RoutePrefix("api/v1/clients")]
public class ClientsController : ApiController
{
// GET: api/Clients/5
[ResponseType(typeof(string))]
[Route("{id:int}/emailid"),HttpGet]
public IHttpActionResult GetClientEmailId(int id)
{
//code
}
}
You said:
In the browser If I type http://localhost:54919/api/v1/clients/?id=1/getemailid it's taking me to http://localhost:54919/api/v1/clients which is not what I want.
From the way your routes are set up, it looks like you need to go to http://localhost:54919/api/v1/client/1/emailid to get to the route you want
To explain the difference, when you call http://localhost:54919/api/v1/clients/?id=1/getemailid the route that would match that is something like:
[Route("api/v1/clients")]
public IHttpActionResult GetClientEmailId(string id)
{
//code
}
because you've added the id parameter as a querystring parameter. In this case, the id argument would have a value of 1/getemailid which doesn't make much sense.
by using the route parameters (by replacing ?id=1/getemailid with 1/emailid) you will actually match the route you want to

How to override web api route on the same method with annotations

I have this configuration in the HttpConfiguration
config.Routes.MapHttpRoute("Default", "api/{controller}");
config.Routes.MapHttpRoute("Another", "api/{controller}/{action}");
config.Routes.MapHttpRoute("WithKey", "api/{controller}/{action}/{key}");
For that reason I cannot access my controller like this
http://<host>/api/products (works)
http://<host>/api/products/1 (doesn't work)
So I added the annotation Route in the get method but it doesn't work
[RoutePrefix("products")]
public class ProductsController : ApiController
{
[HttpGet]
public IQueryable<IProduct> GetProducts()
{
return db.GetProducts();
}
//[Route("products/{productID}")] Tried. Doesn't work
//[Route("{productID:int}")] Tried. Doesn't work
[HttpGet]
public IProduct GetProduct(int productID)
{
return db.GetProduct(productID);
}
}
The only way to make it work is typing the address like this http://<host>/api/products?productID=1, but I'd really want to access with this url http://<host>/api/products/1.
I can add new routes in the http configuration but cannot modify the existing ones. And I don't want to affect existing controllers.
How can I solve this, please?
First ensure that attribute routing is enabled before convention-based routes.
config.MapHttpAttributeRoutes();
//...convention-based routes.
config.Routes.MapHttpRoute("Default", "api/{controller}");
//...other code removed for brevity
Next you want to update the attribute routes.
[RoutePrefix("api/products")]
public class ProductsController : ApiController {
//GET api/products
[HttpGet]
[Route("")]
public IQueryable<IProduct> GetProducts() {
return db.GetProducts();
}
//GET api/products/1
[HttpGet]
[Route("{productID:int}")]
public IProduct GetProduct(int productID) {
return db.GetProduct(productID);
}
}

Route configuration in ASP Core

I Have a problem configuring routes in ASP Core.
In Startup.cs I used default configuration: services.AddMvc() in ConfigureServices() and app.UseMvc() in Configure().
Now I have a simple controller in the same assembly:
[Route("/api/[controller]")]
public class TestController: Controller
{
[HttpGet]
public string Test()
{
return "Hello";
}
}
Request /api/test/test doesn't fire
But if i add [HttpGet("test")] or [Route("test")] it works well.
However I'd like to support convention over configuration in case route attribute is not specified
Try using:
[Route("api/[controller]/[action]")]
When you add [Route("/api/[controller]")] annotation to Controller, the default routing configured in Startup.cs will ignore this Controller.
So you either need to specify URL suffix for each of your actions inside that Controller by adding [Route("")] attribute above them:
[Route("/api/[controller]")]
public class TestController: Controller
{
[Route("test")]
[HttpGet]
public string Test()
{
return "Hello";
}
}
Or specify in Controller annotaion that the routing should use action names as part of URL :
[Route("/api/[controller]/[action]")]
public class TestController: Controller
{
[HttpGet]
public string Test()
{
return "Hello";
}
}
[Route("/api/[controller]")]
public class TestController: Controller
{
[HttpGet(namOf(Test),Name=namOf(Test))]
public string Test()
{
return "Hello";
}
}

Categories