S. Aziz Kazdal
S. Aziz Kazdal

Reputation: 1186

asp.net core 3 routing

In my Asp.net core 3 project I am using some controler to access from js code to do some stuff and also using Razor pages at the same time.

at the configure service section :

    services.AddControllersWithViews();
    services.AddRazorPages();

I added both RazorPages and MVC controller with view.

And at then configure section

        app.UseRouting();
        app.UseEndpoints(endpoints =>
        {              
            endpoints.MapRazorPages();
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");

        });

Added above codes. When I try to access to controller I am getting 404. I also tried to add just services.AddControllers();

need help please.

Edit : controller code

public class DashboardsController : BaseController, IDashboardsController
{
    [HttpGet]
     public async Task<JsonResult> GetAssetCategorySummaryAsync(){
        -------
    }
}

Upvotes: 1

Views: 2434

Answers (2)

user8753842
user8753842

Reputation:

I can recommend my solution.

In Startup

app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
});

Create your CUSTOM base controller like that.

    [Route("api/[controller]/[action]/{id?}")]
    [ApiController]
    public class CustomBaseController : ControllerBase
    {
    }

And use CustomBaseController

 public class TestController : CustomBaseController
    {
        public IActionResult Test()
        {
            return Ok($"Test {DateTime.UtcNow}");
        }
    }

Rout` api/Test/test

Upvotes: 0

Rena
Rena

Reputation: 36645

Your url should be localhost:9011/Dashboards/GetAssetCategorySummary.

You could modify the Startup.cs like below to allow using the whole action name:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc(options =>
        {
            options.SuppressAsyncSuffixInActionNames = false;
        });
        services.AddControllersWithViews();
        services.AddRazorPages();
    }

It is a known issue on github,you could refer to here.

Upvotes: 1

Related Questions