ankur
ankur

Reputation: 4733

why does Asp.net core Web API 2.0 returns Http Error 500

I have moved the api into a different folder structure then the usual offered by templates.

the structure looks like this

API
  Controllers
     LoginController.cs

LoginController has a basic method

[Route("api/[Login]")]
    public class LoginController : ControllerBase
    {
        [HttpGet]
        public ActionResult<IEnumerable<string>> Get()
        {
            return new string[] { "value1", "value2" };
        }
    }

Program.cs

public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }

Startup.cs

public class Startup
    {
        public IConfiguration Configuration { get; set; }
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors();
            services.AddOptions();
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseCors(builder => builder
            .AllowAnyOrigin()
            .AllowAnyMethod()
            .AllowAnyHeader()
            .AllowCredentials());

            app.UseMvc();

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        }
    }

The solution builds fine. when i try to access the page using following url it just sets

localhost is currently unable to handle this request.
HTTP ERROR 500

.

https://localhost:44352/api/login/get
https://localhost:44352/API/Controllers/login/get

Do some settings needs to be added to return the content.

Upvotes: 1

Views: 1038

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239200

You have no default route defined, which is fine, but then you're entirely reliant on each controller and action having attribute routes defined. On your LoginController, you do have a route attribute, but it's not correct. The brackets are for substituting certain route values like area, controller, etc.; it's not an indication that you actual controller name should go in there. In other words, you need either [Route("api/Login")] or [Route("api/[controller]")], where the latter will then be substituted with the controller name, Login, by ASP.NET Core.

Additionally, when using route attributes, action name no longer plays a part. If you don't define a route, it's the same as defining an empty route, i.e. [HttpGet("")]. Therefore, even with the fix to your controller route, the URL to that action would still be just /api/login, not /api/login/get. If you want the get, then you need to set the route to that: [HttpGet("get")].

Upvotes: 3

Related Questions