2

I am creating an API using ASP.NET Core, and below is my controller code.

I have given the controller and Startup code below, along with fiddler request/response log. I am getting a 404 Not Found error. Please advise what am I missing?

[Route("api/[controller]")]
public class UserController : Controller
{
    [HttpPost]
    public string RegisterUser(User newUser)
    {
        // Code to register user in system
        return succecss/failure message

    }
}

I tried suggested attribute routing from https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing but still no luck. I am not sure what I am missing.

Please note that with the Web API template , there is no default routing in Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseMvc();
}

The following is my fiddler request:

URL: http://localhost:8011/api/User/RegisterUser

Verb= post

User-Agent: Fiddler
Host: localhost:8011
Content-Type: application/json
Content-Length: 77

Request body
{"FirstName": "TEST FirstName", "LastName": "Test Last Name", "EmailId":"[email protected]","Password":"1"}

Output in response header:
HTTP/1.1 404 Not Found

2 Answers 2

7

[Route("api/[controller]")] on the controller, together with a [HttpPost] on the action means that the action will be available at /api/[controller]. So in your case /api/User.

Unless you include [action] in the route template, the method name of the action will not be included in the route.

So, try a POST to /api/User, or change the route attribute on the controller to [Route("api/[controller]/[action]")].


Note that the reason the API project template does not include default routes in the Startup is because with APIs you are expected to configure them explicitly. That way, the routes are part of your application design. With RESTful APIs, you are usually designing APIs in a way that they represent “resources”. And then you are using HTTP verbs to indicate what you are actually doing. The included ValuesController is actually a good example for that:

// GET api/values
[HttpGet] Get()
// => Get all values

// GET api/values/5
[HttpGet("{id}")] Get(int id)
// => Get a single value

// POST api/values
[HttpPost] Post([FromBody] string value)
// => Add a new value

// PUT api/values/5
[HttpPut("{id}")] Put(int id, [FromBody] string value)
// => Set/replace a value

// DELETE api/values/5
[HttpDelete("{id}")] Delete(int id)
// => Delete a value

By not having any routing there by default, you don’t accidentally end up with method names within the route (since method names are usually imperative word combinations, making it more RPC instead of REST). Instead, you will have to deliberately expose your methods as actual routes with verbs.

In your exapmle, your UserController would probably be for a “User” resource. So registering a user would probably be a POST request to /api/User.

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

Comments

0

You have not fully defined your route.

[Route("api/[controller]")] creates the /api/User portion of the url, but you have not specified the rest of the route on your action.

So your request should change from to http://localhost:8011/api/User/RegisterUser to http://localhost:8011/api/User

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.