-2

All Links I find talk about using an Web API inside a mvc project.

But I want the opposite.

I can not find any info about that scenario and how to integrate mvc into a web api project.

Is it possible at all?

Elisabeth
  • 20,496
  • 52
  • 200
  • 321
  • Can you explain why you want to mix them? Perhaps an actual example of what you are trying to achieve would help? – The Senator Nov 18 '15 at 19:58
  • Assuming your Web API is hosted in IIS, you can absolutely place them in the same project. Even if you're self-hosting, you still can. This is answerable by googling "web api mvc same project". Every result talks about how to do this. –  Nov 18 '15 at 19:59
  • Although it is certainly possible, the question I want to ask is: why? Why do you want to make it yourself hard if you can make it easy. Visual Studio does a lot of things for you when you create a MVC application. If you choose to ignore this, you'll have to do all those steps by yourself (like initiating the application, configuring the routes, filters, ...) – hbulens Nov 18 '15 at 20:13
  • @Amy Have you read the solution of the link you posted? I don`t think so. I will paste it here:"...below article gives you step by step implementation of ASP.NET Web API in MVC application..." I did NOT ask about using a Web API in a MVC application. I asked how I can use MVC inside a web api project - because we want to use the easy forms handling with razor instead of doing a SPA... which is overhead. – Elisabeth Nov 18 '15 at 21:27
  • @hbulens I can not create a MVC project because we already have a legacy web api. – Elisabeth Nov 18 '15 at 21:28
  • If you create an Web Api using Visual Studio and default setting, it will create a folder called "Areas" under which the help pages resides. This gives a nice mvc setup within an Web Api project. How about using the same infrastructure and develop further? – TejSoft Nov 18 '15 at 23:38
  • @Elisabeth, I didn't paste a link... I think you have a fundamental misunderstanding here. You have an ASP.Net project that uses Web API. You can absolutely add MVC to the mix. You just add the Nuget packages and the configuration, and that's it. What you have isn't "a web api project". What you have is a ASP.Net project *with Web Api*. –  Nov 19 '15 at 01:14
  • this question doesn't really make sense. WebAPI and MVC are not "Projects", they are modules that can be used in a project. – Claies Nov 19 '15 at 02:09

2 Answers2

1

I'm starting these instructions from an empty ASP.Net project setup with Web API.

  1. Install the Microsoft.AspNet.Mvc Nuget package.

  2. Set up MVC routes and any other MVC configuration in your global.asax, where you configure Web API. You can do this configuration in a separate class and call it from global.asax, if you wish.

Example:

RouteTable.Routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
  1. Add an MVC controller to a controllers folder. This should be a separate folder that houses your Web API controllers, and does not have to be called controllers.

  2. Create a Views folder. To make this a lot easier and less time consuming, create an MVC project in a throwaway solution and copy its Views folder over. In particular, you want to copy over any files in the Views\Shared folder, and all .config files inside the Views folder. Without these, your MVC views won't work.

  3. If you created a throwaway project for #4, look through its web.config, in particular, at the <system.web> settings.

If you didn't, here's part of mine:

<system.web>
   <authentication mode="None" />
   <compilation debug="true" targetFramework="4.5.2" />
   <httpRuntime targetFramework="4.5.2" />
</system.web>

You should now have MVC working in a ASP.Net project alongside Web API.

For more organization, you can create an MVC Area. To get started you don't need Areas, and if you decide later to add Areas, it's easy to move existing MVC code into one or more new Areas.

For more information, read this blog post by David Paquette. Yes, the article is about Web Forms, but the instructions and concepts are congruent. Plus, Web Forms is just an ASP.Net project with Web Forms, just like Web API.

-1

It's simple and I use it on several sites - create a new Area (I use 'UI') and treat it as a simple MVC site.

You will have to fix your default routes per this: Multiple types were found that match the controller named 'Home'

Community
  • 1
  • 1
Christopher G. Lewis
  • 4,777
  • 1
  • 27
  • 46