Hi @Newbie ,
GUI i mean the root URL=localhost:(port)/ not show anything after i adding swagger (localhost:port/swagger/index.html works fine) , im forget to check http code respond at the root "/" , i just want to know it's possible or not, thanks for your time.
Have you ever solved the problem? If not, try to re-start the application and refer to the following steps:
- Run the following command to create an Asp.net 6 MVC application.
dotnet new mvc -o MvcSample
: Creates a new ASP.NET Core MVC project in theMvcSample
folder.
code -r MvcSample
: Loads theMvcSample.csproj
project file in Visual Studio Code. - Using the
cd MvcSample
command to go to the application folder. - Run the following command to install the Swashbuckle:
dotnet add MvcSample.csproj package Swashbuckle.AspNetCore -v 6.2.3
. - Add and configure Swagger middleware. After modifying, the program.cs file like this:
var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllersWithViews(); // add service for API builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); var app = builder.Build(); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Home/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } else{ app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); app.Run();
- Add API controller:
Then, run the application usingusing MvcSample.Models; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.Data; using System.Text; // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 namespace MvcSample.Controllers { [Route("api/[controller]")] [ApiController] public class ToDoController : ControllerBase { // GET api/<ToDoController>/5 [HttpGet("{id}")] public string Get(int id) { return "value"; } // POST api/<ToDoController> [HttpPost] public void Post([FromBody] string value) { } // PUT api/<ToDoController>/5 [HttpPut("{id}")] public void Put(int id, [FromForm] string value) { } // DELETE api/<ToDoController>/5 [HttpDelete("{id}")] public void Delete(int id) { } } }
dotnet run
command, and use browser to open the web app, the result as below:
Reference:
Get started with Swashbuckle and ASP.NET Core
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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