I upgrading my project to ASPNET5. I'm hitting a snag regarding upgrading my web.config file.
I tried using Microsoft.Framework.ConfigurationModel.Xml package to read a URL Rewrite configuration. The config looks like:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<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.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
but of course I'm getting duplicate key issues when it tries to convert it to the object that aspnet5 uses.
Whats the best way to port your existing web.config to the new aspnet model? This is a small case I realize but in the real world these configs are really intense.
I've created an example project that I'm hoping to share with others when i get a few of these cases figured out.
the web.config file you posted only has url rewrite rules for iis. Do you really need to access those from your app? IIS will read those directly from the web.config but you shouldn't need that stuff in your app so you don't need to try to parse that file with the new classes in Microsoft.Framework.Configuration at all.
if your app needs some other settings then you might as well use a json.config file as shown in many examples for your own application settings.
you can still drop that web.config file into the application root and assuming your app is hosted in IIS it should still do its job and tell IIS to do some url rewriting. you should not rename it to config.xml as it appears you have done, since IIS won't notice the file unless it is named web.config
why would you need to access those url rewrite rules from application code at all since it is IIS and not application code that does the url rewriting?
I think the new paradigm is use json.config and/or environment variables in azure for application configuration
The only thing you might still use a web.config file for is IIS configuration but that is separate from application configuration and that is the only thing you would use web.config files for going forward and you should not need to access web.config file from application code at all.
Related
I'm currently developing an app in Angular with an ASP.NET backend. I've configured the project to work fine locally in IE11 (Update polyfills, browserlist, and add ES5 TsConfig files), but when I've published it to the server via IIS the page will not load and is stuck at "Loading..."
To test out publishing I've attempted to publish the default Angular/ASP.NET app to the same results.
The console displays four errors:
SCRIPT1002: Syntax error
runtime.458556a34b891ea32398.js (1,1)
SCRIPT1002: Syntax error
polyfills-es5.7119ad0e4b3aeae98a0b.js (1,1)
SCRIPT1002: Syntax error
polyfills.4efda4a4618e08b621be.js (1,1)
SCRIPT1002: Syntax error
main.59969322f93fb24d6bee.js (1,1)
polyfills.ts
import 'classlist.js';
import 'web-animations-js';
import 'zone.js/dist/zone';
web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Angular 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>
<defaultDocument enabled="true">
<files>
<add value="ClientApp/dist/index.html" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
your application may throw errors in IE11, even when it is working fine in other browsers. There can be numerous reasons why your Angular application is not working, including:
Missing polyfills in polyfills.ts.
Using a TypeScript target version which IE11 does not support
Importing third-party dependencies using a TypeScript target version
which IE11 does not support
Refer this : https://medium.com/better-programming/how-to-fix-your-angular-app-when-its-not-working-in-ie11-eb24cb6d9920
Thanks #Deepak-MSFT for the answer, turns out I didn't have the .NET Hosting Bundle installed on the host machine. Once I installed that, the website displayed as it was supposed to -- barring a couple issues not related to what I'm talking about here.
For those curious, I was having issues with database connection string, I found another project being hosted on the server and modified its connection string:
data source=[database];initial catalog=[table];persist security info=True;user id=[user id];password=[password];MultipleActiveResultSets=True;App=EntityFramework
I also removed the web.config file I'd created, as it would probably get in the way of the one generated during publishing.
This morning I pushed a new .Net web site (fw 4.6.1, previously 4.5.1) out to production. IIS (I assume) stripped the extensions from all web pages (ie. index.aspx just became index). I've had these web sites running for 8+ years using IIS rewrites. It looks like then IIS saw the "extensionless" web pages, the rewrite kicked in and sent it to a non-existent location, producing a 404.
I also did a Windows update 3 days ago (could have missed this issue since but unlikely). The rewrite rule basically says, if you can't find the page, look for the page in the "Clients" subdirectory. The rule looks like:
<rewrite>
<rules>
<rule name="Client Relocation" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" negate="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="Clients/{R:0}" />
</rule>
</rules>
</rewrite>
I was able to work around the issue by removing the rewrite rule in a subdirectory:
<rewrite>
<rules>
<remove name="Client Relocation" />
</rules>
</rewrite>
So, it would appear the extension was stripped, then IIS gets the page and thinks it doesn't exist so the rewrite rule takes effect.
So, my question is: who/what is stripping the extension and how can I stop it from doing so?
Lex Li put me on the right track with the comment about the application and failed request tracing. Turned out to be the App_Start/RouteConfig.cs file got modified and added:
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings);
Not exactly sure how or why but commenting out the AutoRedirectMode put the extensions back on. If I could give the answer to Lex's comment, I would have.
This issue is not related to application configurations (custom), but more to do with IIS settings.
So I need the following to be in the web.config when i create a publish for my app.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Redirect to https" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
</rules>
</rewrite>
<security>
<requestFiltering allowDoubleEscaping="true" />
</security>
</system.webServer>
</configuration>
However, when debugging i only want the part and not the http redirect (If i try to debug my app with the rewrite in the web.config it does not start)
in previous asp.net, we could have multiple web.configs for debug and release and it would transform when published.
I simply want to the all of the above code to be in the web.config when published, and only part to be in applied in web.config when i am debugging
This isn't a true answer to your question, but I've got what I think is a much better solution overall. For some time, I've found the fact that URL Rewrites have to go into the Web.config to be frustrating. As careful as you are, it's almost inevitable that you're going to overwrite the Web.config at some point, removing rewrites that have been added to it. This is especially the case if a developer doesn't know better and adds a rewrite directly through IIS, but never copies it over to the project's Web.config in source control (which happens more often than not).
As a result, I started creating a site in IIS just for redirects like this. It has nothing but a Web.config, and then I add the bindings that I'm redirecting from to it. For example, for a rewrite like this, you'd add the binding for the HTTP version of your domain to the redirect site and the HTTPS binding to the actual web application site. Then, you can create the rewrite rule on the the redirect "site", and never ever worry about accidentally overwriting it, because you never publish anything there. This would effectively side-step your issue here, entirely.
I have several shared hosting accounts with Newtek. They told me one account can be used to host several websites, but I don't know how to do that.
They said it can be done using either an .htaccess or web.config file. And since it's ASP.NET, that suggests web.config is the way to go.
Does anyone know how to use web.config to route requests for a given domain to a subfolder? I assume that subfolder would need to be set as an application starting point, which it appears their control center will allow me to do. I just need to associate that starting point with a particular domain.
And does this sound like a reasonable approach (shy of forking out money for a virtual server)?
If the IIS URL Rewrite Module is installed, you can create a Rule to redirect the request to a subfolder based on the domain name.
<rule name="site2.com" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?site2.com" />
<add input="{PATH_INFO}" pattern="^/site2/" negate="true" />
</conditions>
<action type="Rewrite" url="\site2\{R:0}" />
</rule>
Source: https://weblogs.asp.net/owscott/iis-url-rewrite-hosting-multiple-domains-under-one-site
I want to host multiple wordpress sites under the same domain, but different paths in azure web apps.
So on the server I would have a web.config under wwwroot and the wordpresssites under their own directory.
wwwroot - web.confing
| \ wp1
| \ wp2
Currently my web.config looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="wordpress" patternSyntax="Wildcard">
<match url="*"/>
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
</conditions>
<action type="Rewrite" url="index.php"/>
</rule></rules>
</rewrite>
</system.webServer>
</configuration>
and it works as long as all my files are in the wwwroot.
How should I rewrite it so it serves from wp1 when I enter domain.com/wp1 and serves wp2 when I go to domain.com/wp2?
The post "WordPress installation on Root Folder & Sub Folder on IIS Server, url conflict" on Wordpress support site may help you.
Also the SO question "Second Wordpress installation on IIS server" may be helpful.
They both show a configuration where one installation is on the root and one in subdirectory. But these should offer guidance on your setup.
You can find a good walkthrough also from here: http://www.erichstauffer.com/web-design/install-wordpress-root-folder-sub-folder-windows-iis-server-godaddy
I'd recommend leaving the default web.config in each Wordpress folder and create virtual directories in the Azure Portal for wp1 and wp2 - similar to the image below, but instead of blog2, you will have the root and 2 virtual directories /wp1 and /wp2. In the root, put whatever static html or redirects you require: