Ad
Invoke ASP.Net Core MVC Controller Action With Parameter Doesn't Work
I have a simple ASP.NET Core MVC controller action method defined as:
public class MyMVCController : Controller
{
public async Task<IActionResult> MyAction(String usrid)
{
// ...
}
}
When I call it using this URI:
https://localhost:6009/MyMVC/MyAction/073214df
My action gets invoked and I get return back, but the parameter usrid
is always null. What am I doing wrong?
Ad
Answer
It`s because the default route pattern has the following definition:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
Therefore, if change the method parameter name to id
it will work:
public async Task<IActionResult> MyAction(String id)
Or you can apply the following route attribute to the method:
[Route("MyMVC/MyAction/{usrid?}")]
public async Task<IActionResult> MyAction(String usrid)
{
// ...
}
Ad
source: stackoverflow.com
Related Questions
- → How to Fire Resize event after all images resize
- → JavaScript in MVC 5 not being read?
- → URL routing requires /Home/Page?page=1 instead of /Home/Page/1
- → Getting right encoding from HTTPContext
- → How to create a site map using DNN and C#
- → I want integrate shopify into my mvc 4 c# application
- → Bootstrap Nav Collapse via Data Attributes Not Working
- → Shopify api updating variants returned error
- → Get last n quarters in JavaScript
- → ASP.NET C# SEO for each product on detail page on my ECOMMERCE site
- → SEO Meta Tags From Behind Code - C#
- → onchange display GridView record if exist from database using Javascript
- → How to implement search with two terms for a collection?
Ad