Subdomain routing in ASP.NET 5 - asp.net

I am trying to get sub-domain routing working in my ASP.NET 5 application.
I basically want to map http://api.example.com to a particular controller within my application, http://map.example.com to a different controller etc.
I have looked at various articles about this but they are all out of date and do not work with the latest version of the ASP.NET framework.
The official documentation is missing information on routing. Maybe it's my own fault for trying to use a beta product!
Looking forward to hearing some ideas of how to get this working.

If I understand your intent correctly, ASP.NET routing is not the proper way for this.
If this was for ASP.NET versions before 5.0, I'd have suggested that you intercept the request before it's routed within Application_BeginRequest() and check for HTTP_HOST Request Header value to determine which site the user wanted to visit.
I see that the application flow has changed in a major way with vNext. However, I came across this sample from the ASP.NET MVC 6 source on github that creates a custom route based on a "User" header in the request:
https://github.com/aspnet/Mvc/tree/dev/samples/CustomRouteSample.Web
I believe this may be the starting template for a similar solution to your issue if you use "HTTP_HOST" header instead.
Good luck, let us know if you're able to implement a working solution.

Related

Many large projects, routing for image server asp.NET mvc

I work in a company that has over 70 asp.net mvc projects (very similar ones)
All images are linked like this : Url.Content("~/imagefoldername/picname.jpg");
Now I want to merge those 70 projects into one, very similar to Drupals multiple-site per installation system.
I'd like to have all images on one server (maybe with the possibility of even using a CDN)
I'm wondering if it's possible to somehow Route /imagefoldername/picname.jpg to something like
http://img.mycdnname.com/project1/picname.jpg
This way I don't need to iterate trough all views int the 70 projects, but just add a rule to global.asax, and would save me a lot of time.
Is it possible? How do I route it?
You could use IIS URL Rewriting module or just setup separate route in MVC and create an action that will return HTTP 302.
Make sure that you read this article by Phil Haack first.

Use asp.net routing on top of old asp system

I'm experimenting with using MVC routing as a temporary fix to get SEO friendly urls on an old (VB6/ASP classic) system while it's being re-written (which will take a long time).
The old system has 1 asp file with a vb6 dll that generates html which is served by a response.write in the master.asp.
so urls on that system look like this:
www.foo.com/master.asp?sessionid=abc123&pagetype=Item&ItemID=My-widget
I'm wondering if I can use an MVC project to create a route for cleaner urls
and have a controller map the values and build the corresponding old url and then do a Server.Transfer to it.
So the new url would look like:
www.foo.com/Item/My-widget
and map to the old url at
www.foo.com/master.asp?sessionid=abc123&pagetype=Item&ItemID=My-widget
both could then be used interchangeably so the existing site doesn;t have to change, but I could use the new cleaner url on external sites for better SEO
Is this possible?
Is there another way to do this?
edit:
since it's not possible to use server.transfer from MVC, I'm now considering using routing in an ASP.net webforms app.
This should allow me to get the routing part of the application done. Will post back here once I've tried it.
I would suggest you use UrlRewriter.net library instead. It has a lot more features than the the built-in Routing framework (including Regex support), support for permanent redirects, and it's all configurable in the web.config file.
I've tried to use Routing before for this sort of thing but found that it became quite limiting very quickly.
http://urlrewriter.net/
Edit: you will still need a .net web project "wrapper" for your classic asp solution though, as you describe in your question, which of course comes with it's own problems as outlined in the other answers.
I cant think of a way that you could do this, but you might have some luck with the url rewriter module in iis: http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/
I think this could be made to work, but it doesn't sound ideal. I suspect you'd run into some issues with sharing data between the two sessions too. It may be a good idea IF you are planning to migrate to .net, and the app will be running in a "half and half" state for a while (if so I'd suggest introducing a managed core library shared between both sides before worrying about url rewrites).
I haven't done much work with classic ASP lately, but I think this post on URL Rewriting in Classic ASP might be helpful to you.

ASP.NET Friendly URLs

In my research, I found 2 ways to do them.
Both required modifications to the Application_BeginRequest procedure in the Global.Asax, where you would run your code to do the actual URL mapping (mine was with a database view that contained all the friendly URLs and their mapped 'real' URLs). Now the trick is to get your requests run through the .NET engine without an aspx extension. The 2 ways I found are:
Run everything through the .NET engine with a wildcard application extension mapping.
Create a custom aspx error page and tell IIS to send 404's to it.
Now here's my question:
Is there any reason one of these are better to do than the other?
When playing around on my dev server, the first thing I noticed about #1 was it botched frontpage extensions, not a huge deal but that's how I'm used to connecting to my sites. Another issue I have with #1 is that even though my hosting company is lenient with me (as I'm their biggest client) and will consider doing things such as this, they are wary of any security risks it might present.
`#2 works great, but I just have this feeling it's not as efficient as #1. Am I just being delusional?
Thanks
I've used #2 in the past too.
It's more efficient because unlike the wildcard mapping, the ASP.NET engine doesn't need to 'process' requests for all the additional resources like image files, static HTML, CSS, Javascript etc.
Alternatively if you don't mind .aspx extension in your URL's you could use: http://myweb/app/idx.aspx/products/1 - that works fine.
Having said that, the real solution is using IIS 7, where the ASP.NET runtime is a fully fledged part of the IIS HTTP module stack.
If you have the latest version of IIS there is rewrite module for it - see here. If not there are free third party binaries you can use with older IIS (i.e. version 6) - I have used one that reads the rewrite rules from an .ini file and supports regular expression but I cant remember its name sorry (its possibly this). I'd recommend this over cheaping it out with the 404 page.
You have to map all requests through the ASP.NET engine. The way IIS processes requests is by the file extension. By default it only processes the .aspx, .ashx, etc extensions that are meant to only be processed by ASP.NET. The reason is it adds overhead to the processing of the request.
I wrote how to do it with IIS 6 a while back, http://professionalaspnet.com/archive/2007/07/27/Configure-IIS-for-Wildcard-Extensions-in-ASP.NET.aspx.
You are right in doing your mapping from the database. RegEx rewriting, like is used out of the box in MVC. This is because it more or less forces you to put the primary key in the URL and does not have a good way to map characters that are not allowed in URLs, like '.
Did you checked the ASP .Net MVC Framework? Using that framework all your URLs are automatically mapped to Controllers which could perform any desired action (including redirecting to other URLs or controllers). You could also set custom routes with custom parameters. If you don't have seen it yet, maybe it will worth the look.

How to SEO friendly an existing ASP .NET 3.5 web application under IIS6

So, I know there's a lot of this subject here and over the Internet. But most articles/questions refers to "static" url rewriting, like:
www.site.com/products.aspx?category=Books
So they rewrite it to
www.site.com/Products/Books
That's ok but I need something else.
The site is like a CMS, it has different types of content.
Nowadays to read the article titled "How StackOverflow helps you in your development" you need to go to an URL like the following.
www.site.com/viewContent.aspx?Id=1234
What I want to achive is:
www.site.com/Content/Articles/how-to-stackoverflow-helps-you-in-your-development
So as I understand, I need to involve ASP .NET in that, because first I need to retrieve the article (an its title of course) and then rewrite the URL.
But I'm wondering how the hell ASP .NET will know how to get that article if I go to that URL, it doesn't include the id anywhere...
So maybe I could accept something like
www.site.com/Content/Articles/1234/how-to-stackoverflow-helps-you-in-your-development
I'm kind of lost here really.. I've never done any URL Rewriting at all and I've googled a lot and I cannot find a way to do what I want. Maybe what I want is not called url rewriting??? I don't know...
The site is running under Windows 2003 Server, IIS6, ASP .NET 3.5 SP1
And of course, I need a free solution, cannot spend 100usd on the ISAPI mod (besides I don't know if that is going to do what I need).
Thanks to all and sorry if this is a duplicated question, but I couldn't find it.
EDIT: I don't need to support non-ASP.NET files (jpgs, gifs, etc) don't need to be rewritten. I just need to rewrite the viewContent page to include the content title into the URL.
You can use the new Routing that comes with ASP.Net 3.5 sp1 to have clean URLs.
This can even be done in web forms and not just MVC. ( I have done it myself). See here and here for exanples of how to set it up.
You can throw the Id of the article and the title in the URL and make the Id the real parameter that gets used to search for the article. That is what SO does. Try removing the question for the URL and it will take you to the same place.
Even if you don't use the Id you can pass the title of the article "how-to-stackoverflow-helps-you-in-your-development" to your DB and retreive the article based on the title.
With regards to IIS 6 it is a little trickier since IIS 6 by default can't handle extension-less URL's.
There are a few work arounds:
Use the wildcard mapping in IIS to map all requests to Asp.Net
Put Default.aspx at the end of your urls
See this post for other possible solutions.
Although the first solution may have performance issues if all content in your site goes through Asp.Net (even images, css, .html ...) in a small site it shouldn't matter. I have used this approach and there wasn't any major performance issues. I think it is the simplest solution. Here is the website I built with it
I hope this helps.
This one is free, I have used it and it works pretty well: http://www.codeplex.com/IIRF
The nice thing is that it will handle url's without an extension (i.e. .aspx, .html, etc.)
You can achieve this with ASP.NET routing.You can do this with ASP.NET MVC as well as Webforms.No need to do anything with IIS.
Check the below link
https://web.archive.org/web/20201205221404/https://www.4guysfromrolla.com/articles/051309-1.aspx#postadlink
I did URL routing in my web application within 1 hour with the details from the above link.Its quite simple to learn .They provide sample codes too.It will help you to do it easily
You can retrieve the Id of the content using the title.But title should be unique.You can use ajax to check whether the title is a already existing one when user takes mouse out from the textbox.
Easiest way is to add a http module to your current webforms project.
http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
This shows you the basics of it, But it can easily be expanded so that the pages you want to rewite is taken from a database or even built on the fly.
ASP.net MVC is your friend for that
stackoverflow use MVC

How to construct expandable website structure?

HI,
I have a ASP.NET webiste I created from craft and it now look a big mess. I want to reorganize this but don't know the good way to do it. Some first look well but later cause trouble with master page, image path...
Now I'm thinking of 2 ways:
Using UrlWriter: but it seems lead to a bulk of path rewrite and usually lead to Resource not found or something
Using a page as main entry and using Server.Tranfer to pull the right page content, despite of its location
Which is better? Do you have another method?
Please help!
There's another approach, System.Web.Routing, added in ASP.NET 3.5 SP1. Basically, you implement the IRouteHandler interface and manually route the request to an appropriate handler.
This is how ASP.NET MVC handles request routing. There's a guide here that uses it for Web forms.
By the way, consider looking at ASP.NET MVC and check if it's appropriate for your situation.

Resources