Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post data to controller .net core

Tags:

c#

.net-core

I have the following controller:

[Route("api/[controller]")]
[Authorize]
public class AuthenticationController : Controller
{
    private readonly IAuthenticationService _authenticationService;

    public AuthenticationController(IAuthenticationService authenticationService)
    {
        _authenticationService = authenticationService;
    }

    [AllowAnonymous]
    [HttpPost]
    public IActionResult Authenticate([FromBody] UserModel userParams)
    {
        var authenticate = _authenticationService.Authenticate(userParams.Username, userParams.Password);
        return Ok("");
    }

    [HttpGet]
    public IActionResult GetAll()
    {
        var users = "Greta";
        return null;
    }
}

I'm trying to make a post with postman, but I get this result:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Error</title>
    </head>
    <body>
        <pre>Cannot POST /api/Authentication/Authenticate</pre>
    </body>
</html>

enter image description here

Have anyone an idea why I get this error?

I'm using the React-template with Visual Studio. Can it have something to do with that?

When I try to access the GetAll endpoint, I get this:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <meta name="theme-color" content="#000000">
        <base href="/" />
        <!--
      manifest.json provides metadata used when your web app is added to the
      homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
    -->
        <link rel="manifest" href="/manifest.json">
        <link rel="shortcut icon" href="/favicon.ico">
        <!--
      Notice the use of  in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
        <title>ICOM.Cbs</title>
    </head>
    <body>
        <noscript>
      You need to enable JavaScript to run this app.
    </noscript>
        <div id="root"></div>
        <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
        <script type="text/javascript" src="/static/js/bundle.js"></script>
    </body>
</html>

EDIT: It seems to work now. I removed FromBody and then It worked. I also tried with FromForm, and It works. I don't know why.

like image 630
Bryan Avatar asked Aug 01 '26 23:08

Bryan


1 Answers

I suspect you just need to explicitly specify the route for the action:

[AllowAnonymous]
[HttpPost(nameof(Authenticate))]
public IActionResult Authenticate([FromBody] UserModel userParams)
{
    var authenticate = _authenticationService.Authenticate(userParams.Username, userParams.Password);
    return Ok("");
}

Notice the route in the HttpPost attribute, which I just set to the name of the method.

If you just use .UseMvc() in your Startup.cs, then you aren't specifying default routes, so you need to specify the route for each action. If you want default routes to be automatically assigned, then you need to use this in your Startup.cs:

app.UseMvc(routes => {
   routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
});

But even if you do that, if you specify a Route at the controller level, the default routes go out the window, so you must specify the routes at each action in that controller.

More reading:

  • Routing in ASP.NET Core
  • Routing to controller actions in ASP.NET Core
like image 181
Gabriel Luci Avatar answered Aug 03 '26 14:08

Gabriel Luci