15

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.

3
  • What version of mvc/web api ? Commented Jul 12, 2016 at 22:16
  • What about creating a separate MVC project which will consume the webapi project endpoint ? This will enforce clean separation of concerns Commented Jul 12, 2016 at 22:18
  • 1
    For the Core MVC, you only need Nuget "Install-Package Microsoft.AspNet.Mvc" Or create a new Web APi and MVC project and figure out the Refrence difference. Commented Jul 12, 2016 at 22:24

5 Answers 5

17

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?}");             
        });
    }
}
Sign up to request clarification or add additional context in comments.

Comments

12

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 the nuget package manager.
  • 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.

Comments

2

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?}");
    });
    

Comments

2

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

  1. Add services.AddControllersWithViews(); in ConfigureServices

  2. And define MVC Routing in Configure Like

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    

Comments

2

To add mvc in the existing dot net core project

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.