views:

54

answers:

4

I need to fetch the domain name and path from the request to provide the following return values:

domain1.com/default.aspx    returns      domain1.com/default.aspx
domain1.com/                returns      domain1.com/
domain1.com                 returns      domain1.com

At the moment every URL fetch function I try seems to return domain.com/default.aspx no matter what the address bar in my browser says. Any solution to this?

I've tried lots of different built in function to retreive parts of the request but none seem to provide desired results.

A: 

Hi, please provide default document in IIS, and it will work in same manner as you want. so your can set products.aspx in domain1.com's default document and in domain2.com make it categories.aspx and it will be all set.

lakhlaniprashant.blogspot.com
I need a programatic solution not one via IIS
Tom Gullen
A: 

You might want to have a single website within IIS with multiple bindings/host headers for your domains, and remove default.aspx from the default documents. You should then be able to use the urlMappings configuration element to facilitate your redirects. If you need more information on the urlMappings check out this link: http://dotnetperls.com/urlmappings-aspnet

Kane
A: 

You could create an HttpModule for each domain - implement the Application_BeginRequest event like so:

private void Application_BeginRequest(object source, EventArgs e)
{
    // get context and file path
    HttpContext context = ((HttpApplication)source).Context;
    string filePath = context.Request.FilePath;

    if (filePath.Equals("/"))
    {
        // redirect to products page
        context.Response.Redirect(filePath + "products.aspx");
    }
}

This should redirect http://www.domain1.com/ to http://www.domain1.com/products.aspx - requests for http://www.domain1.com/default.aspx should not redirect.

Jonathan
+1  A: 

There is a solution to your problem.

HttpContext.Current.Request.Urlwill return a Uri object that contains all the parts of the URL broken down for you. From that, you should be able to get what you're looking for. Specifically, the property you want is Uri.Authority.

EDIT: Try something like this:

    public static string GetPath(this HttpRequest request)
    {
        var authority = request.Url.Authority;
        var pathAndQuery = request.Url.PathAndQuery;
        var query = request.Url.Query;
        var path = pathAndQuery.Substring(0, 
                       pathAndQuery.Length - query.Length));

        if (!authority.EndsWith("/"))
            authority += "/";

        return authority + path;
    }
Mike Hofer
URI authority only seems to return the domain name.
Tom Gullen
See edit above. (Haven't tested, but should be in the ballpark.)
Mike Hofer