Best practices in using URL Rewriter - asp.net

I have an ASP.NET web application and I'm using the URL Rewriter in IIS. I want to make the URL's simple and meaningful so I created a rule in IIS for every page like this:
MySite/Admin/AddEditStudent.aspx will be "MySite/Student/New"
MySite/Admin/AddEditStudent.aspx?ID={Number} will be "MySite/Student/{Number}"
MySite/Admin/Students.aspx will be "MySite/Students"
MySite/Admin/AddEditBook.aspx will be "MySite/Book/New"
MySite/Admin/AddEditBook.aspx?ID={Number} will be "MySite/Book/{Number}"
MySite/Admin/Books.aspx will be "MySite/Books"
....
....
....
And so on
I have many pages and I'm afraid I might be using the wrong away lthough the above is working fine. I thought there might be a way to use fewer number of rules in a general way.
My second question is about the home page. Currently it's like this:
MySite/Public/Home.aspx will be "MySite/Home"
However, I want it to be only "MySite" without needing to add the "/Home" part. Is it possible to do it this way?
Thanks in advance,
PS: "MySite" is a replacement of the localhost which I made in the host file, but when I deploy the application on production it will be the DNS name.

First Question
In my idea defining the routes table in code is more easier and flexible. You can define the route in Application_Start of Global.asax. Also include namespace System.Web.Routing.
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RouteTable.Routes.MapPageRoute("StoreRoute",
"BookStore/{Name}",
"~/Webpages/BookStore/ViewBookDemo.aspx");
}
So your web.config will be cleaner and easier to setting other configurations.
Second Question
First of all set your default document in web.config on your default page as here:
<configuration>
<system.webServer>
<defaultDocument enabled="true">
<files>
<add value="home.html" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
Now every links whose href is '/' will rewrite your route on default document without showing the home.html in the shown url.
Regards

Related

Rename and/or Change Path to scriptresource.axd

We have a requirement that the root of our site is actually at domain.com/xyz/ and we can't have /xyz/ be a virtual directory. Some modules in our site are leveraging the ScriptManager which is by default trying to reference domain.com/scriptresource.axd?blah=blah. The scriptresource.axd will not be resolved in this case. We have made it so the references to scriptresource.axd are now change before getting sent to the client to be /xyz/scriptresource.axd but this isn't working.
My suspicion is the ScriptResource Handler Mapping is not picking up the call because of the xyz. I have tried to update the Handler mapping using IIS Manager or by editing the \inetsrv\config\applicationhost.config, \frameworkv4.blah\web.config and \framework64\v4blah\web.config but these seem to not work either.
Can someone tell me how I could get the ScriptResource handler to handle something other than /scriptresource.axd.
You can try with urlMappings with something like:
<system.web>
<urlMappings enabled="true">
<add url="~/xyz/default.aspx"
mappedUrl="~/scriptresource.axd"/>
</urlMappings>
...

Redirect a user when he or she tries to navigate to a page in ASP.NET website

Is it possible to re-direct a request for a page 'XYZ.aspx' to 'ABC.aspx', without writing any code? We are using ASP.Net 4.0 with IIS 7.5.
The idea is that we want to remove XYZ.aspx page from our website, and if users still use old bookmarks that point to XYZ.aspx, then we want them to be taken to ABC.aspx in an automated manner.
there seems to be a few ways, link should hopefully help you out.
http://www.trainsignal.com/blog/iis7-redirect-windows-server-2008
you can forward the user through web.config file or you can do it through IIS too. but I I were you I would use web.config. in shared hosting u cannot touch IIS.
I found the answer. One needs to use the following in web config, and that's it.
<location path="XYZ.aspx">
<system.webServer>
<httpRedirect enabled="true" destination="http://localhost/mysite/ABC.aspx" httpResponseStatus="Permanent" />
</system.webServer>
UPDATE : This will also work if any query string parameters are passed to the page XYZ.aspx.

Custom VirtualPathProvider not being used in IIS6

I added the following lines to Application_Start method in global.asax:
var provider = new TestVirtualPathProvider();
HostingEnvironment.RegisterVirtualPathProvider(provider);
Yet the 'TestVirtualPathProvider' is never used when deploying this application in IIS6 (it does in the ASP.NET Development Server).
Edit: the default path provider has always done its job correctly and served (non-embedded) views correctly. The problem is simply that I want to use my own path provider to provide embedded views. So, initially, I already had the following wildcard mapping configured:
Any possible reasons why this does not work in IIS6?
Are there any other factors (handlers for example) wich might influence the used VirtualPathProvider?
UPDATE: the fact that you want to handle extension-less URL's is an important point that's not mentioned in the question. Please see this page for help in setting up MVC with IIS 6: http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx. This should cover your scenario as well.
Most likely the same issue that I answered in this thread: http://forums.asp.net/t/995633.aspx
Basically, add this in your web.config:
<httpHandlers>
<add path="*" verb="GET,HEAD,POST" type="System.Web.StaticFileHandler" validate="true" />
</httpHandlers>
That other thread has some details that explain why this is necessary.
For the combination Custom VPP + IIS6 + Precompiled site, we need to add the VPP from AppInitailize();
public static class AppStart
{
public static void AppInitialize()
{
// code to be executed automatically by the framework
}
}
See also:
http://sunali.com/2008/01/09/virtualpathprovider-in-precompiled-web-sites/
I believe that you need to use an ISAPI filter in IIS6 to intercept URLs without extensions. Problem is that ISAPI will need to be done in c/c++.
IIS6 is configured to allow only certain extensions to be processed by the ASP.net pipeline.
To findout how you can redirct requests check out the post by DocV.

Puzzling wildcard problem in IIS7 & Module

We have an ASP.NET 3.5 app running in IIS6 we're migrating to IIS7 & the integrated pipeline. Our app does some very simple URL rewriting to examine a URL like this:
website.com/dealer/page.aspx
stripping 'dealer' out, looking it up in the DB for context and going to page.aspx.
In IIS6 this was a wildcard map. I moved the module to the right place in web.config for IIS7:
<system.webServer>
<modules>
<add name="ModuleRewriter"
type="Insignia.Catalog2.ModuleRewriter, Insignia.Catalog2"
preCondition="" />
And it works - almost. Paths like these work:
website.com/dealer/page.aspx
website.com/dealer/
The latter defaults to the index.aspx page. My problem is, this one doesn't:
website.com/dealer
note the missing slash at the end. I get a 404. What am I missing?
UPDATE:
It has something to do with the Static file handler - if I disable that, the URL maps correctly, but then static stuff doesn't work...
Well, I got it to work by modifying the modules tag:
<modules runAllManagedModulesForAllRequests="True">
but this is definately cargo-cult-ish because i don't know what it's doing yet.

Extensionless Page Requests

I have a customer of ours that sent out a publication referring to my site but they used the wrong address...they did not provide a page name...it looks like this:
mywebsite.org/Resources/toolkits/bridging
when it should have been
mywebsite.org/Resources/toolkits/bridging/default.aspx
Is there a way to tell ASP.NET to default to this default.aspx when it sees this kind of request or even better, have IIS 7 handle this easily?
This site is live so I would like to avoid having to introduce code if possible.
As per other suggestions, this should be done in the IIS configuration for your website using the IIS Admin tool.
There is however, another alternative - you can add a section in the web.config of your actual ASP.NET application, allowing you to override the IIS configuration right from your application:
<system.webServer>
<defaultDocument>
<files>
<clear />
<!-- Specify each of your files by order of preference here -->
<add value="Default.aspx" />
<add value="Index.aspx" />
<add value="MyOtherPage.aspx" />
</files>
</defaultDocument>
</system.webServer>
The caveat to this though is it may be a little obtuse when the IIS administrator can't figure out why the server configuration isn't working the way he's got it configured. It's not always right to do something just because you can.
Finally, just in case you don't have access to the IIS server or your IIS administrator has reasons for not adding Default.aspx to the default document list in the IIS configuration and for whatever reason, you don't wish to override the IIS configuration in your web.config file, then the quickest and simplest way is to simply create a file called default.asp in that directory containing:
<% Response.Redirect("default.aspx") %>
Default.asp is in the default document list on IIS. The code will automatically redirect the call to the correct page. The downside to this approach though is that there's a performance hit - every time someone calls default.asp - directly or otherwise, the redirect needs to happen which isn't free.
In the Documents tab of the web site properties in IIS you can specify default documents. If you are using .Net2.0 or later on that machine then Default.aspx should already be set....
Default.aspx is not, oddly enough, set as the default document in an IIS installation; In IIS 7, the setting is under "HTTP Features", called "Default Document". Add default.aspx to that list and you should be OK.
If not, you'll need to add a 404 handler that redirects when it sees that URL.

Resources