Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET 5 HTML5 History

I'm upgrading my project to ASPNET5. My application is a AngularJS Web App that uses HTML5 Url Routing ( HTML5 History API ).

In my previous app I used the URL Rewrite IIS Module with code like:

<system.webServer>
  <rewrite>
    <rules>
      <rule name="MainRule" 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_URI}" matchType="Pattern" pattern="api/(.*)" negate="true" />
          <add input="{REQUEST_URI}" matchType="Pattern" pattern="signalr/(.*)" negate="true" />
        </conditions>
        <action type="Rewrite" url="Default.cshtml" />
      </rule>
    </rules>
  </rewrite>
<system.webServer>

I realize I could port this but I want to minimize my windows dependencies. From my reading I think I should be able to use ASP.NET 5 Middleware to accomplish this.

I think the code would look something like this but I think I'm pretty far off.

app.UseFileServer(new FileServerOptions
{
    EnableDefaultFiles = true,
    EnableDirectoryBrowsing = true
});

app.Use(async (context, next) =>
{
    if (context.Request.Path.HasValue && context.Request.Path.Value.Contains("api"))
    {
        await next();
    }
    else
    {
        var redirect = "http://" + context.Request.Host.Value;// + context.Request.Path.Value;
        context.Response.Redirect(redirect);
    }
});

Essentially, I'm wanting to route anything that contains /api or /signalr. Any suggestions on best way to accomplish this in ASPNET5?

like image 954
amcdnl Avatar asked Jul 02 '15 12:07

amcdnl


People also ask

Is ASP NET MVC 5 outdated?

Is the framework outdated? ASP.NET MVC is no longer in active development.

What is HTML5 history API?

The HTML5 History API gives developers the ability to modify a website's URL without a full page refresh. This is particularly useful for loading portions of a page with JavaScript, such that the content is significantly different and warrants a new URL. Here's an example.

When was ASP.NET released?

ASP.NET is an open-source web framework for building web apps on the . NET (dotNET) framework. It is created by Microsoft and version 1.0 was released in 2002 to allow developers to build dynamic web apps, services, and sites.

Is ASP NET MVC spa?

ASP.NET MVC 4 Beta introduced Single Page Applications (SPA), web applications that provide rich functionality to a web page through client side scripting. As of this writing support is limited, but it does provide a basic framework for building data driven web pages using popular JavaScript libraries.


1 Answers

You were on the right track, but rather than sending back a redirect, we just want to rewrite the path on the Request. The following code is working as of ASP.NET5 RC1.

app.UseIISPlatformHandler();

// This stuff should be routed to angular
var angularRoutes = new[] {"/new", "/detail"};

app.Use(async (context, next) =>
{
    // If the request matches one of those paths, change it.
    // This needs to happen before UseDefaultFiles.
    if (context.Request.Path.HasValue &&
        null !=
        angularRoutes.FirstOrDefault(
        (ar) => context.Request.Path.Value.StartsWith(ar, StringComparison.OrdinalIgnoreCase)))
    {
        context.Request.Path = new PathString("/");
    }

    await next();
});

app.UseDefaultFiles();
app.UseStaticFiles();
app.UseMvc();

One issue here is that you have to specifically code your angular routes into the middleware (or put them in a config file, etc).

Initially, I tried to create a pipeline where after UseDefaultFiles() and UseStaticFiles() had been called, it would check the path, and if the path was not /api, rewrite it and send it back (since anything other than /api should have been handled already). However, I could never get that to work.

like image 148
Bill Avatar answered Oct 13 '22 00:10

Bill