Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add MVC to existing web API project

Currently I have a web api project that I want to add an admin page that can create users and modify permissions, however there appears to be virtually no documentation on how to add MVC to an existing web api project.

like image 622
user3376703 Avatar asked Jul 12 '16 21:07

user3376703


People also ask

Can we plug an ASP.NET MVC into an existing ASP NET application?

Luckily, the answer is yes. Combining ASP.NET Webforms and ASP.NET MVC in one application is possible—in fact, it is quite easy. The reason for this is that the ASP.NET MVC framework has been built on top of ASP.NET.


3 Answers

For anyone using .Net Core 3, configure your Startup.cs with this template.

Note, I have commented out the existing Web Api code.

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

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        //services.AddControllers();
        services.AddControllersWithViews();
    }

    // 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.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            //endpoints.MapControllers();
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");             
        });
    }
}
like image 69
tno2007 Avatar answered Sep 29 '22 11:09

tno2007


update for .Netcore 2.1 projects

  1. Install Microsoft.AspNetCore.Mvc through NuGet
  2. Create Views folder (you already have controller folder in your existing API project)
  3. Modify ConfigureServices method in Startup.cs and add this line:

    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    
  4. Modify Configure method in Startup.cs and add default map route:

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
    
like image 20
2 revs Avatar answered Sep 29 '22 10:09

2 revs


I had a similar Problem with a Project using WebApi but not MVC and used the following Approach to add MVC later on:

  • I added Microsoft.AspNet.Mvc as well as Microsoft.AspNet.Web.Optimization to the project using nugget
  • I added a RouteConfig file (as created by the MVC template) to the App_Start folder and executed the method RegisterRoutes in the Global.asax.cs class, again in correspondence to a new MVC project. This allows the Controllers to get called.
  • I added my Controller (Home) and a new View (Index.cshtml). Adding the View created the Shared/_Layout.cshtml as well as the _ViewStart.cshtml files

Once I started the project, it resulted in me getting the following error message:

The view must derive from WebViewPage, or WebViewPage<TModel>

  • After researching the problem I found an article on msdn describing a solution. The missing component is the Web.config file from the Views folder. Again you can copy a file from an existing MVC project
  • In the Web.config file I had to remove the old projects default namespace from the System.web.webPages.razor section and replace it with my own Default Namespace

And with that done I could use the standard MVC Controllers.

like image 36
SJP Avatar answered Sep 29 '22 10:09

SJP