1

I created an empty Web API project and have this:

GlobalConfiguration.Configure(WebApiConfig.Register);

When I create a non-empty Web API project I have this

    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(WebApiConfig.Register); 
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
    RouteConfig.RegisterRoutes(RouteTable.Routes);

I do not need MVC Areas.

I do not need MVC filters.

I do not need Bundling/Minification as this will be handled my client side libraries.

I DO NEED (at first sight) the RouteConfig because I need the MVC HomeController which renders the initial _Layout_cshtml HTML file.

I am doing a Single Page App (AngularJS) + Web API.

Because of the last point above I have to add MVC stuff to my Web API Project.

Is there any possibility (angularJS/other JS libraries) on client side when the browser starts to initially retrieve a html file and render this instead of calling the MVC RenderBody() Method?

Elisabeth
  • 20,496
  • 52
  • 200
  • 321
  • You could try something like:http://www.iis.net/configreference/system.webserver/defaultdocument – calebboyd Jan 05 '14 at 21:20
  • @calebboyd Does this work on IIS Express too? – Elisabeth Jan 05 '14 at 21:25
  • Hmm.. I have never really used IIS express. Normally I just make new sites in IIS and point them to my project folders. – calebboyd Jan 05 '14 at 21:28
  • Do you have to render using cshtml? In my projects where I use webAPI with angular, my angular app loads from static html. – Daniel Tabuenca Jan 05 '14 at 21:32
  • @dtabuenc NO I do not have to render the .cshtml. As I said html is totally fine cshtml is Razor/MVC what I do not want because of the assembly dependencies... You have a code snippet how you do that static html initial file loading with angularjs? Put it in an answer and I probably mark it as solution :p – Elisabeth Jan 05 '14 at 21:44
  • 1
    @calebboyd Forget about the express version. Did not know my windows 8.1 Pro has the 8.x IIS :P – Elisabeth Jan 05 '14 at 23:23

3 Answers3

4

For a SPA with WebApi, you should not let MVC or .NET do anything to the static side of the site. That will just slow everything down and it's completely unnecessary. The words ".Net controller" and SPA should not be used together :)

The setup I use, when I want to test end-to-end with WebApi, is to have a static site in IIS, say mydomain that contains the entire contents of your static site, at least index.html. Then I create a subfolder under that site called "api" that is a full application. This will be your WebApi. Since it's a subfolder under mydomain CORS is no problem, but as far as IIS is concerned the root is pure static, and the "api" folder is a full-blown .Net application.

Here's what it looks like in IIS...

WebApi in Static Site in IIS

Tim Hardy
  • 1,654
  • 1
  • 17
  • 36
3

You could just embed the HTML file as a project resource and return it directly?

public class HomeController : ApiController
 {
   [HttpGet]
   [Route("/")]
   public HttpResponseMessage Get()
   {
     var content = new StreamContent(this.GetType().Assembly.GetManifestResourceStream(this.GetType(),"home.html"));
     content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
     return new HttpResponseMessage() { Content = content };
   }
 }
Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
  • Nice copy/paste of your own ImagesController :P Thanks! – Elisabeth Jan 06 '14 at 12:06
  • @elisa I actually copied from the other answer and wrote the rest of the code from memory. I've written it so many times :-) – Darrel Miller Jan 06 '14 at 12:18
  • Maybe you still want some points http://stackoverflow.com/questions/20950432/the-route-template-on-the-action-named-index-cannot-start-with-a-chara :D – Elisabeth Jan 06 '14 at 12:51
  • 1
    I thought your code would work when I run it... After I fixed the "~/"-Bug I had to find out that the GetManifestResourceStream method how you use it returns null for me. I use the namespace + filename string but still it does not work. Can you tell me please how to make it work? – Elisabeth Jan 06 '14 at 14:06
  • I made it working my way: var stream = File.OpenRead(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Views\Home\Index.html")); but would still be pleased when you would fix your code :) Thank you. – Elisabeth Jan 06 '14 at 14:13
  • @Elisa The way I did it will work if you embed the html file in the same folder as the controller. Otherwise specify a relative path from the location of the controller to the embedded file. The name is case sensitive, replace "/" chars with "." and if your physical path structure below the project does not match your namespaces then it is hard to find the embedded resource. – Darrel Miller Jan 06 '14 at 14:18
  • @Elisa This might help too http://stackoverflow.com/questions/27757/how-can-i-discover-the-path-of-an-embedded-resource – Darrel Miller Jan 06 '14 at 14:19
  • That means there is a dependency to the namespace? But then I can also stick to my AppDomain.CurrentDomain.BaseDirectory approach because there is a dependency to the project structure... one dep. for the other ;-) Anyway you helped me thanks again. – Elisabeth Jan 06 '14 at 14:59
0

Really all you need to do is setup a static HTML file wherever you keep your other static resources (scripts, css, etc).

You can then just redirect to it from your root controller:

 public class HomeController : ApiController
 {
   [HttpGet]
   [Route("/")]
   public RedirectResult Get()
   {
     return Redirect("/contents/myapp.html");
   }
 }
Daniel Tabuenca
  • 13,147
  • 3
  • 35
  • 38
  • you are a funny guy. Actually I wanted to get rid of MVC as I said above but the RedirectResult sits in the System.Web.MVC :P http://msdn.microsoft.com/en-us/library/system.web.mvc.redirectresult(v=vs.118).aspx – Elisabeth Jan 06 '14 at 12:05