0

In ASP.NET Core Web API I am using Attribute routing and I need to move it to conventional routing.

 [ApiController]
 public class HomeController : Controller
 {
    [Route("GetHome")]
    [AllowAnonymous]
    [HttpGet]
    public string Getdeatils([FromBody] myclass cs, string system)
    {
    }
 }

With Attribute routing the URL localhost/GetHome?system=abc works and triggers Getdeatils method.

I am trying to move same URL pattern to startup, but I am unable to achieve it.I have removed [ApiController] from controller class I have tried below code in startup. But it is not working.

 app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
               name: "mycustom",
               pattern: "GetHome",
               defaults: "{controller=Home}/{action=Getdeatils}/{system?}");
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{system?}");
            
        });

URL:localhost/Home/Getdeatils?system=abc - This works fine

URL:localhost/GetHome?system=abc - This is not working.

How this can be achieved without changing the URL format.

1

1 Answer 1

1

Please try with this code:

app.UseEndPoints(endpoints =>
{
  endpoints.MapControllerRoute(name: "mycustom",
           pattern: "GetHome",
           defaults: new {controller="Home", action="GetDetails"});
  endpoints.MapControllerRoute(name: "default",
           pattern: "{controller=Home}/{action=Index}/{system?}");
}

Please let me know if it works as expected.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.