4

I am develop Angular2 App with ASP.Net Core MVC and ASP.Net Web Api.

Here is my basic architecture.

ASP.Net Core MVC Project (MyProject.Web)

  • Nuget, npm, and bower is used for dependencies. Bower is used to copy the npm dependencies from node_modules to wwwroot/libs/
  • Home/Index controller's action delivers the first page which loads Angular2 and other dependencies.
  • Templates are loaded from ActionMethods i.e. Home/About
  • SystemJS loads the modules.
  • Angular2's http service calls the ASP.Net Web Api Project (a separate project than mvc)

ASP.Net Web Api Project (MyProject.Api) - Each Controllers is designated for CRUD operations of an entity, and responses to Angular2's http

Problem: I am not able to use HTML5 routing and am forced to hash routing because html5 routing calls the server and my MVC project does not have the corresponding controller. So server doesn't return anything.

3
  • You should be able to configure your server to support HTML5 pushState. It just needs to return index.html for unknown URLs Commented Jan 14, 2017 at 14:53
  • First, there is no index.html, as mentioned above the action methods are used for delivering html. Second, What if my AngularApp is requesting a resource (i.e. image, css, js file etc) which exists but is available no more and I want to display an error. But in your solution, the index action is returned? How to deal with it? Commented Jan 14, 2017 at 15:18
  • You can return a 404 response (not rewrite to index.html) for requests that try to access resources in the asset directory (or distinguish by other criteria) Commented Jan 14, 2017 at 15:22

3 Answers 3

6

If you plan to use on IIS, you can use URL rewriting module. It basically tells web server to rewrite all routing URLs to index.html. .

<?xml version="1.0" encoding="utf-8"?>
<system.webServer>
   <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule"
resourceType="Unspecified"/>
   </handlers>
   <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%"
stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout"
forwardWindowsAuthToken="false" />
      <rewrite>
         <rules>
            <rule name="Angular 2 pushState routing" stopProcessing="true">
               <match url=".*" />
               <conditions logicalGrouping="MatchAll">
                  <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                  <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                  <add input="{REQUEST_FILENAME}" pattern=".*\.[\d\w]+$" negate="true" />
                  <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
               </conditions>
               <action type="Rewrite" url="/index.html" />
            </rule>
         </rules>
      </rewrite>
   </system.webServer>
</configuration>
Sign up to request clarification or add additional context in comments.

Comments

1

Problem: I am not able to use HTML5 routing and am forced to hash routing because html5 routing calls the server and my MVC project does not have the corresponding controller. So server doesn't return anything.

Adding # into route path is probably the most common solution in a case like you, but...

Do you have any particular reason for using MVC with Angular?


In my opinion, you don't need MVC - you could just create empty ASP.NET Core project and add into your HTML/Angular app - clean, simple, comfortable and no conflicts between MVC and Angular ;)

Add your files (including index.html) into wwwroot and update your Startup.cs file:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
            app.UseDefaultFiles(); <- Add this line before UseStaticFiles
            app.UseStaticFiles();            
    }
}


Please find more information:

12 Comments

This is what I have seen most tutorials say. However, I want to take benefit from Razor as well, (thought I haven't so far, but I am think eventually I will) i.e. I will display the static info (info that barely changes frequently) via ViewModel and will not let angular use http calls for that info. What do you say about that use case?
The answer depends on your design and implementation - if you are going to SPA than Angular will be your main technology and you will not see benefits from MVC.
Of course, you could create MVC app (as the main technology) with Angular as an additional (supporting) technology, but I assume you are not interested that direction.
About your example and will not let angular use http calls for that info - what the problem? Angular or MVC - the same http request.
Currently, I'm working on a project designed to use benefits from both MVC and AngularJS,,, and I didn't notice any use of MVC except overcomplicated design. (Sorry for my English, I hope you understand what I mean).
|
1

"I am not able to use HTML5 routing and am forced to hash routing because html5 routing calls the server and my MVC project does not have the corresponding controller. So server doesn't return anything"

https://github.com/aspnet/JavaScriptServices

Look for the Routing helper aka spa services nuget and the spa fallback route.

When no serverside route is found and the route has no file extension then the index.html should be returned to the client where the current route is interpreted as a client side route by angular router.

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.