asp.net core 3.1 change route - c#

Is there an easy way to change the route of asp.net core 3.1 controller ?
Currently I have controller PicVideosController URL: ...\picvideos... and I was asked to modify the url to ...\picturesvideos...
I added a route on the controller side :
[Route("picturesvideos")]
public class PicVideosController : Controller
get an error AmbiguousMatchException: The request matched multiple endpoints. Matches: Main.Controllers.PicVideosController.Pay (Main) Main.Controllers.PicVideosController.Completed (Main) still seems like it is looking at the original url

try this:
[Route("picturesvideos/[action]")]
public class PicVideosController : Controller
{
[Route("/")]
[Route("~/picturesvideos")]
[Route("~/picturesvideos/index")]
public IActionResult Index()
....

Related

Is there an OOTB component to get routes from controller names and their methods?

I would like to list all the available endpoints a controller provides.
Is it possible to access the component(s) .NET uses to generate these routes (by, for instance providing it a type or controller name (string))?
The methods/verbs (so, POST, GET) are not even that important for my scenario, just the routes themselves.
Example
Please, take a look on the below ASP.NET Core code.
[ApiController]
[Route("[controller]")
public class HomeController : ControllerBase
{
[HttpGet("additional")]
public async Task<IActionResult> Whatever()
{
// ...
}
}
So, the method will be exposed as a GET endpoint on the URL of Home/additional.

Areas do not work when migrating an Angular app to NET 6

I have an .NET Core 5 with Angular app and I have my controllers grouped in areas. I made the app using NET Core 1 and have successfully migrated it up to 5 without any problems, but migrating it to NET 6 gives me a 404 errors when I make API calls.
My current NET 5 setup looks like this:
[Authorize]
[ApiController]
[Route("[area]/[controller]/[action]")]
public abstract class BaseController : Controller
{
}
[Area("Home")]
public abstract class HomeController : BaseController
{
}
public class AccountController : HomeController
{
[HttpGet]
public async Task<IActionResult> GetSomething()
{
}
I created a new project in VS2022, copied everything, made the changes in Program.cs and changed BaseController to inherit ControllerBase.
The angular app works OK, but all my API calls return 404.
I didn't even have this in the NET 5 app, but I added it now just in case:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name : "areas",
pattern : "{area:exists}/{controller=Home}/{action=Index}/{id?}"
);
});
but still no luck.
EDIT:
Using this answer, I listed all the routes and do get this:
{area: 'Home', controller: 'Account', action: 'GetSomething', name: null, template: 'Home/Account/GetSomething'}
and I still have no idea why it doesn't work.
Because of the proxy you have to list all the areas PROXY_CONFIG in proxy.conf.js. Add the area, or if you're using controllers, the controller names to the context property, eg:
context: ['/AdminApi', '/HomeApi' ... ]
Or, as a workaround, add /Api before all your calls and then have just that in the proxy settings

How to correctly receive string in url format in ASP NET Core 3.1?

Lets say we have an API that receives URLs in one of its controllers actions. How is this doable in Net Core 3.1? If i understand correctly, then https://www.test.com/anothertest/test will mess up the routing to the controller?
Code example below
[Route("api/[controller]")]
public class TestController : Controller
{
[HttpPost("{url}")]
public ActionResult<string> Post(string url)
{
// I want this to work! The url should be the full url specified i.e. https://www.myawesome.com/url/with/slashes
}
}
So if i call https://localhost:5001/api/Test/https://www.url.com/with/slashes i would get https://www.url.com/with/slashes as the incoming url argument.
If you pass https://www.url.com/with/slashes as part of the url,"/" will be unrecognized.
Normally, the easiest way to pass the url is through querystring.
You can change your code like bellow.
[HttpPost]
public ActionResult<string> Post(string url)
{
//...
}
Then the url should be
https://localhost:xxxxx/api/test/?url=https://www.url.com/with/slashes

Remove controller name from URL with Attribute routing

I want to remove controller name from URL for specific Controller.
My Controller name is Product
I found some link to do this
Routing with and without controller name
MVC Routing without controller
But all the above links done in route config file. and those are affecting other controller too. I want to do it using Attribute Routing.
Can it is possible? As I want to do this for only Product controller.
I have tried to do it on action like this
[Route("Sample/{Name}")]
but it is not working.
Gabriel's answer is right, however, it can be a bit misleading since you're asking for MVC and that answer is for Web API.
In any case, what you want is to put the annotation over the class definition instead of an action method. MVC example would be like:
[RoutePrefix("SomethingOtherThanProduct")]
public class ProductController : Controller
{
public ActionResult Index()
{
...
return View();
}
}
I'm also dropping this as an answer since you may find the following article helpful: [Attribute] Routing in ASP.NET MVC 5 / WebAPI 2
Make sure you set the RoutePrefix attribute on the whole controller class, as well as using the Route attribute on the action.
[RoutePrefix("notproducts")]
public class ProductsController : ApiController
{
[Route("")]
public IEnumerable<Product> Get() { ... }
}

Access to controller method

I have Controller with some method GET :
public class TestController : ApiController
{
public List<T> Get(){...}
[ActionName("GetById")]
public T Get(int id){...}
}
Can i access second Get method as /Get?id=1 even if i have different ActionName?
ActionName for generating cache with different names
Updated because my previous answer was related to standard MVC controllers not Web API because that is what the ActionName attribute is for. I am not sure what if anything it would do on a web api controller. Without attributes or a change from the deault routing your actions would have the following routes "/api/test/" Get() "/api/test/id" Get(int id) where id is an int.
If you want more flexibility MVC5 supports attribute routing

Categories