4

I want to set URL Masking in asp.net to hide page name and querystring in URL.

Currently i am setting below code to perform url rewriting in Global Application File.

routeCollection.MapPageRoute("Login", "Login", "~/frmLogin.aspx");

But i want to rewrite URL in a way that it will show only domain name to end user. http://www.domainname.com - like this

Please help me to set it.

Thanks in advance,

Chirag
  • 317
  • 7
  • 19
  • Setting up your page as default page should be best as suggested in answers below, considering performance perspective. – Arvin Jan 19 '16 at 09:34

6 Answers6

4

You can set frmLogin.aspx page as default page in web server.

If you are using IIS 7, steps are as follows:

 1.Select your website
 2. In description click on default document
 3. Add your page (frmLogin.aspx) in and set its priority.
Anupam Singh
  • 1,158
  • 13
  • 25
  • Chirag - this seems perfect for your scenario. Default page is served whenever someone writes your domain name in their browser. It is best from performance perspective as it comes very early in IIS processing pipeline. Try it.. If you have a different IIS version you can always google (or) ask us if you need further help. Please make sure you mave your page to the top of IIS' default page list. – Arvin Jan 19 '16 at 09:30
2

We're using the following method in our application to show the sign-page simply on the domains. It can be modified for other pages as well.

In Global.asax:

routeCollection.MapPageRoute("SIGNIN", String.Empty, "~/signin.aspx");
navigator
  • 1,678
  • 16
  • 29
  • Setting up default page should be less costly than mappageroute, though both should work. – Arvin Jan 19 '16 at 09:32
1

If you use domain masking then there are no code changes and you achieve the same result.

smoore4
  • 4,520
  • 3
  • 36
  • 55
1

I tried the below method for EXPERIMENT PURPOSE. So I don't know how it will behave on complex pages with post back.

when you request www.domainname.com the actual request will go to www.domainname.com /default.aspx or any other default page that you have set. In default page load the first thing is to check for any session named say 'pagetoview' if it is set then server.transfer to that page else server the default page.

Now lets say a user goes to form.aspx' from the page. the form.aspx load method should check the pagetoview session variable if its same to the current page name then unset it and continue else set the pagetoview variable to the current page name and redirect to domain.

there the default page will check and server.transfer will occur. hope you get some point with this weird method.

Ratna
  • 2,289
  • 3
  • 26
  • 50
1

you should simulate asp.net routing with cookie and user controls. so we have just one aspx file named default.aspx and other pages should place into user controls. put this script at the end of default.aspx :

<script src="https://code.jquery.com/jquery-2.2.0.min.js" type="text/javascript"></script>
<script>
    $(document).ready(function () {
        $("a").click(function (e) {
            e.preventDefault();
            var attrHref = $(this).attr("href");
            $.getJSON("/service.asmx/SetRouteCookie", { href: attrHref }, function (e) {
                window.location.reload();
            });
        });
    });
</script>

this script disable all links behavior then we handle click event manually. in click event we call a web service method by ajax. this service set a certain cookie to hold current page:

[WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json, XmlSerializeString = false, UseHttpGet = true)]
    public void SetRouteCookie()
    {
        if (HttpContext.Current.Request.QueryString["href"] != null)
        {
            string href = HttpContext.Current.Request.QueryString["href"];
            HttpCookie c = new HttpCookie("CurrentRoute", href);
            c.Expires = DateTime.Now.AddHours(1);
            HttpContext.Current.Response.Cookies.Add(c);

            HttpContext.Current.Response.ContentType = "application/json";
            HttpContext.Current.Response.Write("{\"status\":\"ok\"}");

        }
    }

after creating cookie and successful callback we reload page by javascript. on default Page_Load event we load appropriate user control:

protected void Page_Load(object sender, EventArgs e)
    {
        #region process route

        if (HttpContext.Current.Request.Cookies["CurrentRoute"] != null)
        {
            var route = HttpContext.Current.Request.Cookies["CurrentRoute"].Value.ToString();
            string pageName = GetPageName(route);
            Placeholder1.Controls.Add(LoadControl("/ctrls/" + pageName + ".ascx"));
        }
        else
        {
            Placeholder1.Controls.Add(LoadControl("/ctrls/default.ascx"));
        }

        #endregion

    }

    public string GetPageName(string href)
    {
        int index = href.IndexOf("&");
        if (index == -1)
            return href.Trim('/');
        else
        {
            return href.Substring(0, index).Trim('/');
        }
    }

I created sample code on git: HideRoute

1

You should use Server.Transfer method for example you have asp.net button in default.aspx write event click like this :

 Server.Transfer("/login.aspx?q1=testQuery");

with this method your url doesn't change and in login.aspx your can get you query string