Hi @Elado,
I don't understand this property in "launchSettings.json" -
"launchUrl": "somestringforurl",
Is it only the name of the URL nothing else ?nothing opens my "ValuesController.cs" file.
The launchUrl
property in the "launchSettings.json" specify the URL for a page that you want to open when you start debugging your project.
For example, when we create a new Asp.net Core API application, by default the launchUrl
property value is swagger
, when we running the application, the result as below:
After adding the ValuesController API controller, and change the launchUrl
property value to api/values
, the result as below:
[Note] we are running the application via IIS express. More detail information about the launchSettings file, you could check Development and launchSettings.json
The ValuesController as below:
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET: api/<ValuesController> // api/values
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/<ValuesController>/5
[HttpGet("{id}")] //https://localhost:44319/api/values/6
public string Get(int id)
{
return "value: "+ id.ToString();
}
// POST api/<ValuesController>
[HttpPost]
public void Post([FromBody] string value)
{
}
}
How to use [HttpPost], [HttpGet] in ASP.NET Core Web API
To answer this question, I suggest you check the Startup.cs file first, as you can see that I didn't add any other route template.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebAPISample", Version = "v1" });
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebAPISample v1"));
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
To access the Values API methods, the request URL as below:
https://localhost:44319/api/values/
=> access the HttpGet method without parameter
https://localhost:44319/api/values/6
=> access the HttpGet method with parameter.
When the action contains the [HttpGet]
or [HttpPost]
attribute (without the template string), it restricts the matching to only HTTP GET or Post requests.
When using the HTTP attribute with template string, for example: [HttpGet("{id}")]
, therefore id is appended to the "api/[controller]" template on the controller. The methods template is "api/[controller]/"{id}"". Therefore this action only matches GET requests for the form /api/test2/xyz,/api/test2/123,/api/test2/{any string}, etc.
If we set the attribute as [HttpGet("apimethod/{id}")]
, the methods template is "api/[controller]/apimethod/"{id}"", and then the action request url will be: https://localhost:44319/api/values/apimethod/6
If we set the attribute as [HttpGet("/apimethod/{id}")]
, the methods template is "/apimethod/"{id}"", and then the action request url will be: https://localhost:44319/apimethod/6
The [Route]
attribute is similar, because all the HTTP verb templates are route templates.
You could refer this sample screenshot:
More detail information, you can check the Routing to controller actions in ASP.NET Core (check the Attribute routing with Http verb attributes part).
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion