简体   繁体   中英

I can't access methods in net core MVC controllers

I can not access my public methods in controller classes. This is my controller:

[Route("authentication")]
public class AuthenticationController : Controller
{
    [HttpGet("example")]
    public IActionResult Example()
    {
        return Ok("This is the Welcome action method...");
    }
}

And also I tried this attribute as well:

[Route("[controller]")]
public class AuthenticationController : Controller

when I try to navigate to localhost:PORT/authentication/example I am getting 404. I am not using API. I am trying to build a web application with .net core MVC and angular. So I will be just sending GET or POST requests to controllers.

This is my program.cs file

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllersWithViews();

var app = builder.Build();

app.UseStaticFiles();
app.UseRouting();

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

app.Run();

I strongly believe that something is wrong in my program.cs. But I couldn't figure it out.

EDIT: If I create an empty ASP.NET CORE WEB APP MVC, I can make it working. I am having problem when I am using MVC with Angular. There might be a problem with SPA proxy as well.

You need to decorate your controller with method / routing attributes

Try:

[Route("api/[controller]")]
public class AuthenticationController : Controller
{
   [HttpGet("example")]
   public IActionResult Example()
   {
      return Ok("This is the Welcome action method...");
   }
}

This will create a get endpoint which can be called at api/authentication/example

Returning a 200 status with the text in the body.

The convention is that if Your memers start with an action verb, it can find out automatically, like

public string GetExample()

However you do not want to return raw string, you always want to return an action result, because you want wrapping with explicit HttpStatus response codes, so

public IActionResult<string> GetExample()

Now many of us a bias towards the works by magic because of prefix and like to be more explicit, not only because the attribute notation allows more control, but also for consistency. Because nearly almost always, at least one action method of the controller actually requires that fine grain.

[HttpGet("example")]
public IActionResult<string> Example()

Then often for instance there is an id and you can go

[HttpGet("example/id?")]
public IActionResult<string> Example([FromRoute] string id)

if you want to not have it go through all the places it might be getting your variables from for instance, there are many choices available

you can try this for example, it will work for localhost:PORT/authentication/example

[Route("[controller]/[action]")]
public class AuthenticationController : Controller
{
   
    public IActionResult Example()
    {
        return Ok("This is the Welcome action method...");
    }
}

//or 

public class AuthenticationController : Controller
{
   [HttpGet("~/Authentication/Example")]
   public IActionResult Example()
   {
      return Ok("This is the Welcome action method...");
   }
}

but since you are Controller as a base class, not ApiController for example, everything should be working if you remove all attribute routing at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM