Configure site root URL in ASP.NET MVC - asp.net

When I print the site root URL in a Razor view like this...
#Url.Content("~/")
... I get the wrong path, e.g.:
/mydomain.org/
How can I change/reconfigure my Web.config so that the path is correct? E.g.
/
The application is installed on a shared hosting in a virtual subdirectory.

<system.web>
<urlMappings enabled="true">
<clear />
<add url="~/" mappedUrl="http://www.example.org" />
</urlMappings>
<system.web>
I am not sure if this works. Maybe something to try though

Related

How to disable Web directory indexing in IIS using web.config file?

How to enable/disable web directory indexing in IIS 7? I am not able to find web.config file. But I can find applicationHand.config file where I saw this line: <directoryBrowse enabled="true" />
I created web.config file in the same folder and added the following. It's not working.
<configuration>
<location path="Secured">
<system.webServer>
<directoryBrowse enabled="true" />
</system.webServer>
</location>
</configuration>
One more question I have is, is by default web directory indexing is disabled in IIS and Apache2 ?

Forward domain alias to a certain page

We have a domain (domain.com) that has an alias (alias.com). We are using Plesk and a Windows Server. In Plesk, alias.com is setup as an alias for domain.com.
We need that when people access to alias.com it goes to a certain page within the main domain, for example domain.com/this-page.html.
The web site is an ASP.NET MVC web site, in case we can do something using the web.config.
Is this possible? How can we do this?
Open web.config in the directory where the old pages reside
Then add code for the old location path and new destination as follows:
<configuration>
<location path="services.htm">
<system.webServer>
<httpRedirect enabled="true" destination="http://example.com/services" httpResponseStatus="Permanent" />
</system.webServer>
</location>
<location path="products.htm">
<system.webServer>
<httpRedirect enabled="true" destination="http://example.com/products" httpResponseStatus="Permanent" />
</system.webServer>
</location>
</configuration>

Default Document not trying all files in IIS 8.5

I have a website with the default settings for default document on IIS 8.5 Windows 2012 R2.
<system.webServer>
<defaultDocument enabled="true">
<files>
<add value="Default.aspx" />
<add value="Default.htm" />
<add value="Default.asp" />
<add value="index.htm" />
<add value="index.html" />
<add value="iisstart.htm" />
</files>
</defaultDocument>
</system.webServer>
When I browse to a URL without a file name (mysite.com/Content/) IIS will only try default.aspx and no other files in the list (index.html). IIS tries to load default.aspx and I get a 404. The strange thing is there is no default.aspx file in that directory and index.html does exist. If I browse directly to index.html the page displays properly. According to the Microsoft documentation on default document it should try the next file in the list until it finds one that exists.
I explicitly added a web.config file to the sub directory with
<defaultDocument enabled="true">
<files>
<clear />
<add value="index.html" />
</files>
</defaultDocument>
and IIS is still trying to use default.aspx. Has anyone encountered this before? Do you have any suggestions as to why this is not working as documented?
Try removing/disabling the old URL rewrite module? (temporarily)
If you are requiring authentication, make sure the login page exists.
(other suggestions copied from above, which I have included, in case someone
else reads this question)
Check for any routing in the source-code.
Check any special modules or handlers, which might cause redirects or intercept calls.
One method for making default document directive to work is:
Go to IIS/Application Pools
Change the ‘Managed Pipeline Mode’ to Classic mode instead of Integrated Mode.
This worked for me.
If you disable default documents but have enabled directory browsing, IIS returns a directory listing when a request for the root directory arrives from a browser. If you disable both default documents and directory browsing, IIS sends an "HTTP 404 File Does Not Exist" error to the browser.
So please enable default documents option in IIS and reset IIS.
If you want use index.html as a default page. you need to click on iis default website after that you can click on default document. Now! you can add index as default page.

Trying to secure all aspx files in a folder secure by IP address

I like to secure all aspx files in a folder ~/Secure/ secure such that specific IP addresses can access the folder's aspx files. I added the following web.config file to the folder, hoping that it adds to the parent web.config:
<configuration>
<system.webServer>
<security>
<ipSecurity allowUnlisted="false">
<clear/>
<add ipAddress="192.168.100.1" />
<add ipAddress="169.254.0.0" subnetMask="255.255.0.0" />
</ipSecurity>
</security>
</system.webServer>
</configuration>
The problem is that I get this error when I try to access to any of the aspx pages in the folder:
This configuration section cannot be used at this path. This happens when the section is locked at a parent level. Locking is either by default (overrideModeDefault="Deny"), or set explicitly by a location tag with overrideMode="Deny" or the legacy allowOverride="false".
What does it take to make this idea happen? I like to just include one web.config file to a folder and that enforces the IP address authorization. I like this idea, since it is no-code and config only.
You cannot do it in the website web.config only.
If you can use IIS manager:
Open IIS Manager, locate the site, click on the folder you want to protect, then click on IP address and Domain Restrinctions.
Also click on "Edit feature settings" in the right Actions panel" to specify actions for unspecified clients (i.e. Deny with Forbidden, or simply Deny With Not Found).
This will generate the right configuration for you.
In your root web.config use the location element:-
<location path="Secure">
<system.webServer>
<security>
<ipSecurity allowUnlisted="false">
<clear/>
<add ipAddress="192.168.100.1" />
<add ipAddress="169.254.0.0" subnetMask="255.255.0.0" />
</ipSecurity>
</security>
</system.webServer>
</location>

asp.net url rewriting with IIS integrated pipeline mode

I am new to ASP.NET. I created a web application and decided to use url rewriting. I tried several solutions like http://urlrewriting.net and http://urlrewriter.net/index.php/support/configuration
These solutions worked fine on my localhost. But when I uploaded it to a shared hosting service provider, all my web pages get 500 internal server errors.
THe web hosting provider told me HttpModules and HttpHandlers are incompatible with IIS Integrated Pipeline mode. They said I'm supposed to move my settings into system.webServer...I tried doing that but must have messed up somewhere because I now get 404 errors. Can someone tell me how to get url rewriting to work for my scenario? Here's what my original web.config looks like:
<configSections>
<section name="urlrewritingnet"
restartOnExternalChanges="true"
requirePermission ="false"
type="UrlRewritingNet.Configuration.UrlRewriteSection,
UrlRewritingNet.UrlRewriter" />
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.0"></compilation>
<httpModules>
<add name="UrlRewriteModule"
type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter" />
</httpModules>
</system.web>
<urlrewritingnet
rewriteOnlyVirtualUrls="true"
contextItemsPrefix="QueryString"
defaultPage = "default.aspx"
defaultProvider="RegEx"
xmlns="http://www.urlrewriting.net/schemas/config/2006/07" >
<rewrites>
<add name="Rewrite" virtualUrl="^~/([^\/]+)/(\d+)$"
rewriteUrlParameter="ExcludeFromClientQueryString"
destinationUrl="~/$1.aspx?id=$2"
ignoreCase="true" />
<add name="Rewrite" virtualUrl="^~/(search|administrator|Default|logout)$"
rewriteUrlParameter="ExcludeFromClientQueryString"
destinationUrl="~/$1.aspx"
ignoreCase="true" />
</rewrites>
</urlrewritingnet>
I think what they are saying is
1) You have to use the rewriter in .NET
2) You have to set it up to use the URL Rewriter, which sits under system.webServer, not system.web.
If I am correct, they are using the URL Rewriter: http://www.iis.net/download/urlrewrite
NOTE: They may not allow your custom HTTP handler (yes, I know it is a published third party, but ISPs are funny like that).

Resources