Dynamically Generating sitemap.xml - asp.net-mvc

I've implemented a sitemap that is dynamically generated on the fly using the technique described here.
Since my site is a WebForms app, my sitemap URL ends up being www.mydomain.com/Sitemap.aspx.
This is working well, and I can use search engine webmaster tools to tell them the URL to my sitemap. However, smaller search engines may not know where to find this file. It would be better if my dynamically generated sitemap could be at www.mydomain.com/sitemap.xml. This is the standard URL for a sitemap.
What is the easiest way to generate the same sitemap data I do now but have it appear as the file sitemap.xml. Can I do it using routing, or do I need a custom HTTP handler?
I'm using webforms but also have sites using ASP.NET MVC so I'm very interested in finding the best technique to do this in both platforms.

For Webforms, you can use HttpModule, check out UrlRewriting.NET
For MVC, use routing:
routes.MapRoute("Sitemap",
"sitemap.xml",
new { controller = "Home", action = "Sitemap" })

For Webforms, you can also update your web.config as mentioned above, you just use plain aspx or ascx page and then do magic with rewrite :-) (I use .net 4.0)
e.g.
<system.webServer>
<rewrite>
<rules>
<rule name="Main Sitemap" stopProcessing="true">
<match url="^sitemap.xml"/>
<action type="Rewrite" url="sitemap/sitemap.aspx" appendQueryString="false"/>
</rule>
</rules>
</rewrite>
</system.webServer>

Related

Beautiful Url and Umbraco routing

I have created a page in Umbraco 7.
I need to pass an id to this page and I want to read that parameter inside the template.
If I use this url: http://www.example.com/product?id=10 I have access to the id inside template using Request["id"] which is fine. but the issue is I want to have my Url like http://www.example.com/product/10 but if I use it I receive: "404 - File or directory not found.".
I actually thought it might be resolved by Umbraco default routing but it is not.
I also tried IIS URL rewrite, but I believe it should be a better way in Umbraco to handle it same as ASP.NET MVC Routing.
You could use this rewrite rule to handle the routing before the request reaches Umbraco.
<rule name="Product Rewrite" stopProcessing="false">
<match url="product/([0-9]+)/"/>
<action type="Rewrite" url="/product/?id={R:2}"/>
</rule>
Another approach is to implement a custom ContentFinder which enable you "find" the content on your own. This allows you make even prettier urls for your products if you can determine the ids from the name in some way. This link https://24days.in/umbraco-cms/2014/urlprovider-and-contentfinder/ provide some information on how to implement a ContentFinder.

301 Redirect Classic ASP requests to ASP.NET MVC 5

I've been given the task of figuring out how to handle 301 redirects for a Classic ASP site to a new ASP.NET MVC 5 application. The domain name will remain the same.
Example of what I need to do:
Old Url:
http://www.example.com/index_cityname.asp
New Url (MVC):
http://www.example.com/cityname
The system will most likely be using IIS 8.x. I'm developing on a Windows 8.1 machine and I've enabled ASP in the Features. But when I enter an .asp extension in the browser, I get a 404.
I built a custom component that analyzes the incoming request and then checks a list and if it exists, it maps the new Response.RedirectPermanent(newPath, true) to the new path. The only problem is, it never gets hit. Somewhere earlier in the lifecycle the application sees that it's an .asp request, and exits.
Any ideas on how to make 301 redirects work from Classic ASP to ASP.NET MVC 5 using the same domain name?
Try adding the following to your web.config file:
<modules runAllManagedModulesForAllRequests="true" />
It belongs under system.webServer and configuration.
But beware performance and other issues using this setting. See http://www.britishdeveloper.co.uk/2010/06/dont-use-modules-runallmanagedmodulesfo.html
You can redirect directly by using rules in web.config. I tried to write up your use case, but maybe you have to tweak the matching rule or target action to suit your needs.
Add this to your web.config:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="redirect classic asp" stopProcessing="true">
<match url="index_(\w+)\.asp$" />
<action type="Redirect" url="{R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I can't go into all details of web.config rewrite rules, but the best (easy) documentation I could find is here: Learn.IIS Rewrite Module
There is also a reference here: Rewrite Module Reference

Azure web site - URL Rewrite using web.config not working

We are migrating some sites over to Azure and I have it setup as a "Web Site".
An example of what we're trying to do is pretty straight forward.
Current URL: http://www.website.com/Products.aspx
Clean URL: http://www.website.com/Products/
So in the web.config, there is the following:
<system.webServer>
<rewrite>
<rules>
<rule name="ProductList">
<match url="/Products.aspx" />
<action type="Redirect" url="/Products" />
</rule>
</rules>
</rewrite>
</system.webServer>
This was generated in IIS. I have tried to set the stopProcess attribute to true... which is a solution that I've seen in a few threads, but nothing is working.
This website is one of our legacy sites that uses an ISAPI module for Rewrites. Since we aren't using an Azure VM, we cannot continue using this method to rewrite.
Is there something I'm missing here?
Azure Web Sites does not have the URL Rewrite module installed, so the config approach won't work. Maybe you could update the site code to use ASP.NET URL routing, as described in this blog post by Scott Gu: http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx
Azure Web Sites didn't used to have the URL Rewrite Module installed, but now it does, so you can just use the standard <system.webServer><rewrite> section in your web.config.

Convert ASP.NET Webforms url to MVC route

I'm replacing an old ASP.NET webforms app with a new MVC application. However, I have an issue with users having old links to a particular page that I would like to automatically translate to the correct MVC route.
Old site: http://mysite.com/ticketsdetail.aspx?id=12345
New site: http://mysite.com/tickets/details/12345
Is there a way in the MVC routing of catching the old url and translating it to the new one?
EDIT:
Ok, the web.config entry to do this using the IIS7 URL rewriting is:
<rewrite>
<rules>
<rule name="Ticket page redirect" stopProcessing="true">
<match url="ticketsdetail.aspx$" />
<conditions>
<add input="{QUERY_STRING}" pattern="id=(\d*)$" />
</conditions>
<action type="Redirect" url="Calls/Tickets/{C:1}" appendQueryString="false" redirectType="Temporary" />
</rule>
</rules>
</rewrite>
Don't do it in code is my suggestion, unless you absolutely need to. Scott Hanselman has a good article on how to do what you need in the web.conf using IIS Url Rewrite.
Article Here
This would be your rule that would go in your web.config as well:
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^ticket/details/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="ticketdetails.aspx?id={R:1}" />
</rule>
If your using IIS7 then the URL rewrite tool is pretty slick;
http://learn.iis.net/page.aspx/460/using-url-rewrite-module/
This is a good article;
http://learn.iis.net/page.aspx/496/iis-url-rewriting-and-aspnet-routing/
Which Option Should You Use?
What does all this information mean if
you need to choose a technology to
enable clean URLs for your Web
applications? In this section, we
explain how to make this choice.
If your Web application is built by
using anything except ASP.NET, use the
IIS URL Rewrite module. Otherwise, the
rules are:
If you are developing a new ASP.NET Web application that uses
either ASP.NET MVC or ASP.NET Dynamic
Data technologies, use ASP.NET
routing. Your application will benefit
from native support for clean URLs,
including generation of clean URLs for
the links in your Web pages. Note that
ASP.NET routing does not support
standard Web Forms applications yet,
although there are plans to support it
in the future.
If you already have a legacy ASP.NET Web application and do not
want to change it, use the URL Rewrite
module. The URL Rewrite module lets
you translate search engine-friendly
URLs into a format that your
application currently uses. Also, it
lets you create redirect rules that
can be used to redirect search engine
crawlers to clean URLs.
However, its logical to me to get the url rewriter to sort out this kind of stuff out at the iis level, especially since you'll be needing to use a regex to pull the querystring apart to form the new url.. Something the url rewriter does amazingly well.

How do I keep subdomain.domain.com links from mapping to subdomain.domain.com/subdomain?

I have a virtual directory created and a sub domain that points to that virtual directory. My links always route to subdomain.domain.com/subdomain/controller/action when they can leave off the subdomain link. Is there an easy way to stop that?
Also, it's the same problem when I mapped anotherdomain.com to my virtual directory. It ends up linking to anotherdomain.com/virtualdir/controller/action.
It just looks unprofessional to me to have all my links be myapp.com/myapp/action/controller.
I have also wondered this, but I don't believe that you can achieve this in the ready-made routing that comes with ASP.NET MVC.
Thankfully, the user has a great deal of freedom in choosing how they want to handle these things, and a solution that you may want to consider can be found here.
The ASP.NET MVC team appear to have recognised that this should be pre-baked into MVC by default, but I don't see anything being done about until v2 as it will require changes to the routing engine.
Failing this, you may want to consider url rewrites (this is obviously much easier if you have IIS7). Here is an IIS forum post discussing this.
IIS 7 Rewrite Example (taken from here):
<rule name="RewriteSubdomain">
<match url="^(.+)">
<conditions>
<add input="{HTTP_HOST}" type="Pattern" pattern="^([^.]+)\.myapp\.com$">
</conditions>
<action type="Rewrite" url="{C:1}/{R:1}" />
</rule>

Resources