GowthamanSS
GowthamanSS

Reputation: 1474

Append virtual path to incoming url c#

Is it possible to change the upcoming url so as localhost/test?t=gowtham to localhost/test/t/gowtham ?

based on my understanding i thought of doing this by extending

public class Myhandlers : IHttpHandlerFactory
{ 
  public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
  {
    string s= application.Request.QueryString["t"];
    PageParser.GetCompiledPageInstance(url,"/t/"+s, context);
  }
}

Am I in right path but I could not achieved it? Or else is there any other way?

Upvotes: 0

Views: 463

Answers (1)

Jag
Jag

Reputation: 753

Whenever I've done stuff like this it's been using HttpContext.RewritePath(). The quick 'n dirty method is to put it in globabl.asax, under beging request. You can use Request.Url to get parts of the requested URL, modify it as you wish, then call RewritePath. Something like this:

void Application_BeginRequest(object sender, EventArgs e)
{
    string Authority = Request.Url.GetLeftPart(UriPartial.Authority);   // gets the protocol + domain
    string AbsolutePath = Request.Url.AbsolutePath;                     // gets the requested path
    string InsertedPath = string.Empty;                                 // if QS info exists, we'll add this to the URL

    // if 't' exists as a QS key get its value and contruct new path to insert
    if (Request.QueryString["t"] != null && !string.IsNullOrEmpty(Request.QueryString["t"].ToString()))
        InsertedPath = "/t/" + Request.QueryString["t"].ToString();

    string NewUrl = Authority + InsertedPath + AbsolutePath;
    HttpContext.Current.RewritePath(NewUrl);
}

Once you're happy that it's working as expected you can stick it in an HttpModule.

NB: Sorry for incomplete code, not at dev machine and cant' remember all the Request.Url parts. Intellisense should help out though :)

Upvotes: 1

Related Questions