Skip to content
This repository was archived by the owner on Nov 1, 2018. It is now read-only.

Issue: URL Rewrite using web.config is broken in RC2 #192

Closed
anwalkers opened this issue May 25, 2016 · 37 comments
Closed

Issue: URL Rewrite using web.config is broken in RC2 #192

anwalkers opened this issue May 25, 2016 · 37 comments
Labels

Comments

@anwalkers
Copy link

I have tried moving the web.config to and fro and cannot get the URL rewrite to work in IIS Express from Visual Studio 2015. If there was middleware to perform what is spelled out in the web.config snippet I could punt on the web.config and selfhost. Maybe someone can point me in that direction to solve our problem.

We need to use Angular 1.x HTML 5 routing in our application and this is how we had solved it in RC1 but it doesn't work in RC2. Others are having trouble as well: http://stackoverflow.com/questions/37304113/url-rewriting-in-asp-net-core-rc2#

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
    <rewrite>
      <rules>
        <rule name="AngularJS Routes" 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}" pattern="api/(.*)" negate="true" />
          </conditions>
          <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>
@guardrex
Copy link
Contributor

I'm going to ping @ciel on Slack, who may have a solution for this.

@ciel
Copy link

ciel commented May 25, 2016

I'll take a look at this when I get home this evening, sure. I'm using
angular extensively with RC2. I know I've got a working demo of this in one
of my projects somewhere.
On May 25, 2016 11:13 AM, "Luke Latham" [email protected] wrote:

I'm going to ping @ciel https://github.com/Ciel on Slack, who may have
a solution for this.


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#192 (comment)

@ciel
Copy link

ciel commented May 25, 2016

I'm not at a computer right now, but looking at your sample have you tried using "^(.*)$" as the match url? And of course, you can confirm that you've got html5mode active?

@anwalkers
Copy link
Author

I haven't tried the regex "^(.*)$". Any links to your examples would be great!

@muratg
Copy link
Contributor

muratg commented May 26, 2016

@anwalkers Were you able to verify the regex suggested by @ciel?

@anwalkers
Copy link
Author

@muratg I have not. I am on a deadline and have turned HTML5 routing off temporarily. Will tackle this when I get to the Azure WebApp deployment step. Hoping @ciel will have a link to an example along the way.

That said, any progress on getting IIS Express from VS 2015 to pay attention to URL rewriting in the web.config?

@anwalkers
Copy link
Author

anwalkers commented May 27, 2016

I tried the following code in my Startup.cs to do middleware URL rewrite. It works fine for the base URL, but does not support deep links into an AngularJS 1.x application with HTML5 routing enabled. It does work with HTML5 routing disabled.

Works:
Base URL: http://localhost:5000
API: http://localhost:5000/api/someApi
Library or resource: http://localhost:5000/lib/library.js
HTML5 Routing Disabled: http://localhost:5000/#/angularRoute NOTE: Deep linking works here.

Does not work:
HTML5 Routing Enabled: http://localhost:5000/angularRoute NOTE: This does work if you have loaded the application from the base URL and navigated to the route, but does not work if you have pasted the deep link with the angular route, http://localhost:5000/angularRoute, into a new browser window.

From Startup.cs:

            app.UseMvc();

            // this serves "index.html" from the wwwroot folder when 
            // a route not containing a file extension is not handled by MVC.
            app.MapWhen(context =>
            {
                var path = context.Request.Path.Value.ToLower();
                return path.Contains(".*");
            },
                branch =>
                {
                    branch.Use((context, next) =>
                    {
                        context.Request.Path = new PathString("/index.html");
                        return next();
                    });

                    branch.UseStaticFiles();
                });

Just for clarity, here is a snippet for enabling HTML5 routing in angular 1.x application. It removes the # hash based routing.

            $locationProvider
                .html5Mode(true);

Reference: https://docs.angularjs.org/guide/$location#!

@muratg
Copy link
Contributor

muratg commented May 27, 2016

@Tratcher thoughts on why this may have regressed?

cc @pan-wang

@anwalkers
Copy link
Author

anwalkers commented May 29, 2016

I have published a simple sample app to this repository.

DELETED THIS REPO: https://github.com/anwalkers/AngularUrlRewriteAspNetCore1

This site is a vanilla ASP.Net Core 1 RC2 project using one MVC route to an index.cshtml as the root page for the site. It does not have the mapWhen function or static html in the wwwroot directory. It is checked in a broken state. You can switch it back to non-HTML5 mode by editing the site.js and a bit of markup.

It also does not have mapWhen in the Startup.cs, wasn't sure how to mix the MVC routing and the mapWhen... It does have the rewrite rules in the web.config.

@anwalkers
Copy link
Author

@muratg @Tratcher @pan-wang @ciel Any progress or discussion on this issue?

@Tratcher
Copy link
Member

Tratcher commented Jun 3, 2016

I was able to repro this using just the UrlRewrite samples here:
http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module
(against ANCM rather than Asp.Net 4).

I see two issues:

  • Kestrel receives the request, but the url has not been modified.
  • Kestrel's response is not forwarded to the client, the client only receives an empty 200 response (Content-Length: 0)

Assigning to @pan-wang to debug.

@WhittenMike807
Copy link

Has anyone found a work around?

@ciel
Copy link

ciel commented Jun 6, 2016

Unfortunately I haven't
On Jun 6, 2016 6:09 PM, "Michael Whittenburg" [email protected]
wrote:

Has anyone found a work around?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#192 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AAavtJ85Mf7Z36px_PWpZ0USzHGFCazyks5qJKiRgaJpZM4ImD44
.

@Tratcher
Copy link
Member

Tratcher commented Jun 6, 2016

I did find one where you can change type="Rewrite" to type="Redirect", but I doubt that's relevant to you here.

@pan-wang
Copy link
Contributor

pan-wang commented Jun 7, 2016

Looking at it now

@anwalkers
Copy link
Author

FYI... Found this project with some middleware to solve the url rewrite problem. Something like this would make a nice addition to ASP.Net Core 1.

https://github.com/BrainCrumbz/AWATTS/tree/master/src/WebPackAngular2TypeScript/Routing

@pan-wang
Copy link
Contributor

pan-wang commented Jun 8, 2016

this is a bug in aspnetcore module. The fix will be included for next release.

@Tratcher
Copy link
Member

Tratcher commented Jun 8, 2016

@pan-wang can you provide a brief explanation please?

@Jargon64
Copy link

Jargon64 commented Jun 9, 2016

@anwalkers I'm trying to see if I can get that middleware implemented into my project as a workaround but appears to be for RC1. Did you try it with an RC2 project?

@pan-wang Don't suppose you know when this release is expected to drop?

@WhittenMike807
Copy link

I was able to use this as a quick workaround.

routes.MapRoute("spa-routes", "{*anything}", new { controller = "Home", action = "Index" });

@Jargon64
Copy link

Jargon64 commented Jun 9, 2016

@WhittenMike807 I think this is working for me (have run into the next hurdle while upgrading to RC2).

Are there any known pitfalls of taking this approach?

@WhittenMike807
Copy link

I haven't encountered any issue so far. Seems like a "ok" fix for now

@JohnGalt1717
Copy link

FYI, this also blows up and prevents debugging from starting. (works fine in production)

<rewrite>
  <rules>
    <rule name="Redirect HTTP to HTTPS" stopProcessing="true">
      <match url="^(.*)$"/>
      <conditions>
        <add input="{HTTPS}" pattern="^OFF$"/>
        <add input="{HTTP_HOST}" matchType="Pattern" pattern="^localhost(:\d+)?$" negate="true" />
        <add input="{HTTP_HOST}" matchType="Pattern" pattern="^127\.0\.0\.1(:\d+)?$" negate="true" />  
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther"/>
    </rule>
  </rules>
</rewrite>

@WhittenMike807
Copy link

@Tratcher Why is this closed? Has it been updated/fixed?

@Tratcher
Copy link
Member

Yes, this has been fixed for the upcoming release.

@WhittenMike807
Copy link

@Tratcher Just so I can plan. You have a rough idea on when the next release will be by chance?

@Tratcher
Copy link
Member

@Cowlephant
Copy link

Cowlephant commented Jun 30, 2016

@Tratcher My URL rewriting is still broken with the RTM release. Wasn't this intended to be fixed?

@Tratcher Tratcher reopened this Jun 30, 2016
@Tratcher
Copy link
Member

@twilliamsgsnetx yes this was fixed in the 1.0 version of AspNetCoreModule. Have you've installed the new tooling?

@Cowlephant
Copy link

Cowlephant commented Jun 30, 2016

@Tratcher I have uninstalled all my previous .NET Core stuff and installed all the new versions, including visual studio tooling.

Could IIS Express be utilizing an older version still somehow? How do I validate my version?

I haven't changed anything in my URL rewriting logic from RC1 either, I don't know if this fix requires any slight modifications.

@Tratcher
Copy link
Member

Look in (MySolution).vs\config\applicationhost.config and find <add name="AspNetCoreModule" image="%IIS_BIN%\aspnetcore.dll" />. If it says %IIS_BIN%\aspnetcore.dll you should be fine. This maps to C:\Program Files (x86)\IIS Express\aspnetcore.dll who's file version should be 7.1.1967.

@Cowlephant
Copy link

Cowlephant commented Jun 30, 2016

Yes. Mine is set that way, and my file is the version you specified.

For clarification, my project's web.config is as follows

<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" forwardWindowsAuthToken="true" stdoutLogEnabled="false" />
  </system.webServer>
</configuration>

And my wwwroot config that has the url rewriting logic is as follows:

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <!--Redirect selected traffic to index -->
        <rule name="Index Rule" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_URI}" matchType="Pattern" pattern="^/api/" negate="true" />
            <add input="{REQUEST_URI}" matchType="Pattern" pattern="^/account/" negate="true" />
          </conditions>
          <action type="Rewrite" url="/index.html" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

This was all working handily in RC1 with Angular 2.

@Tratcher Tratcher removed the 3 - Done label Jun 30, 2016
@muratg muratg removed this from the 1.0.0 milestone Jun 30, 2016
@Tratcher
Copy link
Member

@ZestyBread can you try this?

@jkotalik
Copy link
Contributor

The web.config file doesn't exist in wwwroot anymore. Try combining the two together like:

<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" forwardWindowsAuthToken="true" stdoutLogEnabled="false" />
    <rewrite>
      <rules>
        <!--Redirect selected traffic to index -->
        <rule name="Index Rule" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_URI}" matchType="Pattern" pattern="^/api/" negate="true" />
            <add input="{REQUEST_URI}" matchType="Pattern" pattern="^/account/" negate="true" />
          </conditions>
          <action type="Rewrite" url="/index.html" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

@Cowlephant
Copy link

@ZestyBread Consolidating to my web.config in the main project as you suggested, my entire app breaks. The client files aren't served properly and angular simply doesn't load.

The rewrite section is specifically what seems to be causing it. I comment that out and everything is fine, except the inability to refresh the site on an angular router URL properly.

I hope I'm not the only one experiencing this issue.

Resource interpreted as Stylesheet but transferred with MIME type text/html:"http://localhost:1412/app/content/css/normalize.css". localhost/:8
Resource interpreted as Stylesheet but transferred with MIME type text/html:"http://localhost:1412/app/content/css/main.css". localhost/:10
Resource interpreted as Stylesheet but transferred with MIME type text/html:"http://localhost:1412/lib/css/vendors.css". shim.min.js:1
Uncaught SyntaxError: Unexpected token < zone.js:1
Uncaught SyntaxError: Unexpected token < Reflect.js:1
Uncaught SyntaxError: Unexpected token < system.src.js:1
Uncaught SyntaxError: Unexpected token < systemjs.config.js:1
Uncaught SyntaxError: Unexpected token < vendors.js:1
Uncaught SyntaxError: Unexpected token < (index):23
Uncaught ReferenceError: System is not defined

@Tratcher
Copy link
Member

Tratcher commented Jul 1, 2016

Ah, I know what's wrong here. We moved web.config and the app base dir, so rules with file constraints like matchType="IsFile" are looking in the wrong directory.

Workaround: for IIS you can move the web.config back to the wwwroot dir. #164

Real solution: @ZestyBread is writing a url-rewrite middleware so we can move this directly into Asp.Net.

@muratg
Copy link
Contributor

muratg commented Jul 5, 2016

Closing this one. aspnet/BasicMiddleware#43 is tracking the middleware work.

@muratg muratg closed this as completed Jul 5, 2016
jkotalik pushed a commit that referenced this issue Dec 8, 2017
* fix the AV in recycle process. this is due to we call Recycle again when the background thread exists

* more fixes

* reset hosting mode when all applications got removed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests