Configurable route prefix for controller - asp.net-core

I'm using ASP.NET Core 6 and trying to have the base path of my API controller be configurable (so that users can choose the base path themselves to avoid conflicts with other controllers).
I tried setting up the following route:
string configurablePrefix = "/temp";
endpoint.MapControllerRoute(
name: "MyRouteName",
pattern: configurablePrefix + "/{action=MyDefaultAction},
defaults: new { controller = "MyController" });
Where MyController is defined like this:
[ApiController]
public class MyController : ControllerBase
{
[HttpGet("MyDefaultAction")]
public IActionResult MyDefaultAction()
{
return new JsonResult("Hello");
}
}
This causes no errors during startup, but when I access `https://localhost/temp/MyDefaultAction I get a 404
How can I get this to work so that actions in MyController are accessible on whatever start path the user chooses (i.e. change it to respond to /othertemp/MyDefaultAction instead)?

From your code, you are using ApiController, which cannot be routed by setting the corresponding route in Startup.cs or Program.cs.
ApiController must have attribute routing, and attribute routing has a higher priority than conventional routing, so it will override the conventional routing you defined.
You can choose to use attribute routing to define the controller name as temp, so that the corresponding endpoint can be matched in ApiController:
[Route("temp")]
[ApiController]
public class MyController : ControllerBase
{
[HttpGet("MyDefaultAction")]
public IActionResult MyDefaultAction()
{
return new JsonResult("Hello");
}
}
Test Result:
Or use an MVC controller:
public class MyController : Controller
{
[HttpGet]
public IActionResult MyDefaultAction()
{
return new JsonResult("Hello");
}
}
Routing:
string configurablePrefix = "/temp";
endpoint.MapControllerRoute(
name: "MyRouteName",
pattern: configurablePrefix + "/{action}",
defaults: new { controller = "My", action = "MyDefaultAction" });
Test Result:
reference link: Create web APIs with ASP.NET Core.

Related

Why does not working version in asp.net core controller

I want to use api version in my .net core project.Then search web and find that's solution.
Even though do exactly all solutions,but I can't get desired result.
So if any can help me,Please show me..
I add Microsoft.AspNetCore.Mvc.Versioning 4.0.0 Package in my project and ..
StartUp.cs
Then in my Controller Add Rout Attribute as Shown :
[ApiController]
[Authorize]
[Route("v{version:apiVersion}/[Controller]")]
[ApiVersion("1.0")]
public class SellerController : Controller
{
private readonly IBus _client;
private readonly string AppBaseUrl = MyHttpContext.AppBaseUrl;
//private readonly IGetUrl _globalUrl;
public SellerController(IBus client/*, IGetUrl globalUrl*/)
{
_client = client;
//_globalUrl = globalUrl;
}
[HttpGet("/Sellers/{SellerId}")] // Dashboard
public async Task<IActionResult> Info(long SellerId)
{
...
}
}
With these code I expected that I can send request to 'Info' method by this url :
But that's not working and get 404 error code status.. when I delete "/v1.0" from url and send request, that's working. I will be glad to help me .. Thanks
In your code, we can find that you applied [HttpGet("/Sellers/{SellerId}")] with route
template begin with / to Info action method, which don't get combined with route templates applied to the controller. To make request to 'Info' method, you could use below URL.
https://localhost:5090/sellers/17
I expected that I can send request to 'Info' method by this url : https://localhost:5090/v1.0/sellers/17
To achieve your requirement, you can try to modify the code like below.
[HttpGet("/v{version:apiVersion}/Sellers/{SellerId}")]
public async Task<IActionResult> Info(long SellerId)
{
//...
//for testing purpose
return Ok(SellerId);
}
Test Result
Update:
If you'd like to include v{version:apiVersion} in route template of controller level attribute routing, you can try to apply [HttpGet("{SellerId}")] to Info action method and make request with https://localhost:5090/v1.0/seller/17.
[ApiController]
[Authorize]
[Route("v{version:apiVersion}/[Controller]")]
[ApiVersion("1.0")]
public class SellerController : Controller
{
[HttpGet("{SellerId}")] // Dashboard
public async Task<IActionResult> Info(long SellerId)
{
//...

How do I get Route attribute to work in .net core api?

So I added a controller to a blank asp.net CORE Api.
public class IsAliveController : ControllerBase
{
[HttpPost]
[HttpGet]
[Route("isalive")]
public object Get()
{
return "I'm alive";
}
}
Then I try to access it via https://localhost:44361/isalive
and I get no response. Do I need to make some modifications to my Startup.cs?
First why have you declared your method both as a get and a post request? you can remove the post attribute.
By default your routing must start with your controller name, add this:
[Route("[controller]")]
[ApiController]
public class IsAliveController : ControllerBase
{
[HttpGet]
public object Get()
{
return "I'm alive";
}
}
Now when you call https://localhost:44361/isalive you will receive the expected response.
There are two solutions to your problem:
#1 Your controller Name is IsAlive and you have added a route attribute on your action method as isAlive, so this would work only if you call using this Url
https://localhost:44361/isalive/isalive
#2 Remove the Route attribute from the action method
public class IsAliveController : ControllerBase
{
[HttpGet]
public object Get()
{
return "I'm alive";
}
}
and you will be able to access using Url
https://localhost:44361/isalive
Found the issue. The template I choose (using Visual studio 2019) was:
Asp.net core web app > Web Application
The startup file in this template is missing a line that I needed to add:
app.UseEndpoints(endpoints =>
{
//Add this line
endpoints.MapControllers();
});
After adding that line the endpoint works without any changes to the controller.

Asp.Net Core Api Default Routing

I have a very basic Asp.Net Core Api; my controller looks like this:
[ApiController]
[Route("[controller]")]
public class TestController : ControllerBase
{
[HttpGet("{id}")]
public IEnumerable<Resource> Test(string id)
{
// Breakpoint here
}
I would expect the following URL to invoke the method, and fire the breakpoint:
https://localhost:5012/test/test/1
However, it doesn't. In fact, the following URL does:
https://localhost:5012/test/1
I was under the impression that the format for the URL was as follows (from startup):
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
});
So, unless that action is Index:
https://localhost:5012/controller/action/id
But it appears that the accepted format is:
https://localhost:5012/controller/id
My question is, why is this?
In addition to pwrigshihanomoronimo answer,
you can just change this
[HttpGet("{id}")]
public IEnumerable<Resource> Test(string id)
to
[HttpGet("[action]/{id}")]
public IEnumerable<Resource> Test(string id)
Actually it is ApiController attribute, who breaks your routing. Looks like app.UseEndpoints configures routes just for MVC. So the solution is to remove all attributes to have the following code
public class TestController : ControllerBase
{
public string Test(string id)
{
return "OK";
}
}
Or, if you want to keep ApiController attribute, you would need to adjust Route value as well. You can remove app.UseEndpoints, if you don't use MVC in your project
[ApiController]
[Route("[controller]/[action]")]
public class TestController : ControllerBase
{
[HttpGet("{id}")]
public string Test(string id)
{
return "OK";
}
}

How can I create routes for "web" and "api" in asp.net core

I want to create routes for rendering views (web routes) and routes for administration purpose (api routes). So I created a directory structure like this:
controllers //root controller folder
ProductController
Api //subfolder of controllers folder
Productcontroller
I have classes with the same name and it is messing up my routes. The "api routes" are overlapping the "web routes".
This is how I defined my api routes
namespace Loja.Controllers.Api
[Produces("application/json")]
[Route("api/Produtos")]
public class ProdutosController : Controller
{
// GET: api/<controller>
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
This is how I defined my web routes :
namespace Loja.Controllers
{
public class ProdutosController : Controller
{
[Route("/produtos/{slug}")]
public IActionResult Get(string slug)
When I try to generate a link to my web route I am directed to the api route. Why?
<a asp-controller="Produtos" asp-action="Get" asp-route-slug="#produto.Slug">
When I click the link I am directed to
http://localhost:5000/api/produtos?slug=assadeira-grande-40-x-27-cm-109dcc
One solution is to use areas and create a hyperlink like below:
<a asp-area="Public" asp-controller="Home" asp-action="About">Test</a>
Or you could manually create the a href;
Link
Change the [Route("/produtos/{slug}")] in your Web controller like below
namespace Loja.Controllers
{
public class ProdutosController : Controller
{
[Route("/produtos/Get/{slug}")]
public IActionResult Get(string slug)

Unable to call WebApi 2 method

I've added a webapi 2 controller to my project, inside api > LoginAPi as shown here:
Inside LoginApi I have the following:
[RoutePrefix("api/LoginApi")]
public class LoginApi : ApiController
{
// GET api/<controller>/5
public string Get(int id)
{
return "value";
}
}
Inside my global.asax file I have:
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
Inside App_Start I have the following:
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 }
);
}
I then put a break point inside the Get method within LoginAPI and run the project and type the following into the URL:
http://localhost:37495/api/LoginApi/4
But I get :
No HTTP resource was found that matches the request URI 'http://localhost:37495/api/LoginApi/4'.
So I thought OK let me specify the method name as so
http://localhost:37495/api/LoginApi/Get/4
This returns:
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
Now I've been looking at this for a while so maybe I've missed something obvious, but if someone can please tell me what I'm doing wrong I'd very much appreciate it.
The routeTemplate you have set up would work for convention-based routing except for the fact that Web API adds the string "Controller" when searching for the controller class (as per this article). You therefore need to rename your controller class LoginApiController in order for the convention-based routing to work.
For attribute-based routing, the addition of the RoutePrefix attribute should be combined with a Route attribute on your action. Try adding the following to your Get method in your controller:
[HttpGet]
[Route("{id}")]
And then navigate to http://localhost:37495/api/LoginApi/4.