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.
-
What version of mvc/web api ?Shyju– Shyju2016-07-12 22:16:12 +00:00Commented 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 concernsHassan– Hassan2016-07-12 22:18:20 +00:00Commented Jul 12, 2016 at 22:18
-
1For 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.LeY– LeY2016-07-12 22:24:42 +00:00Commented Jul 12, 2016 at 22:24
5 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?}");
});
}
}
Comments
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
update for .Netcore 2.1 projects
- Install Microsoft.AspNetCore.Mvc through NuGet
- Create Views folder (you already have controller folder in your existing API project)
Modify
ConfigureServicesmethod in Startup.cs and add this line:services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);Modify
Configuremethod in Startup.cs and add default map route:app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); });
Comments
I had a similar Problem with a Project using WebApi but not MVC and used the following Approach to add MVC later on:
Add services.AddControllersWithViews(); in ConfigureServices
And define MVC Routing in Configure Like
app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); });
