Asp.net monolithic mvc project and adding OpenAPI

Newbie 26 Reputation points
2022-08-31T15:37:31.57+00:00

Hi I'm a newbie, i have confusion about adding OpenAPI to empty MVC prj ("dotnet new mvc"), just tinkering around my thoughts, i'm able to success adding Swagger to controllers, appears SwaggerUI, doc... But when i run the app seems like web GUI not working anymore, maybe it's silly to do or think it like that way, or it's the problem of the codes i'm not modified properly,..
I would appreciate some help to explain it and point me at right direction.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,118 questions
{count} vote

Accepted answer
  1. Zhi Lv - MSFT 32,006 Reputation points Microsoft Vendor
    2022-09-01T02:23:33.283+00:00

    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:

    1. 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 the MvcSample folder.
      code -r MvcSample: Loads the MvcSample.csproj project file in Visual Studio Code.
    2. Using the cd MvcSample command to go to the application folder.
    3. Run the following command to install the Swashbuckle:
      dotnet add MvcSample.csproj package Swashbuckle.AspNetCore -v 6.2.3.
    4. 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();  
      
    5. Add API controller:
      using 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)  
              {  
              }  
          }  
      }  
      
      Then, run the application using dotnet run command, and use browser to open the web app, the result as below: 236668-1.gif

    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


0 additional answers

Sort by: Most helpful