Want to show only query string in url routing asp.net - asp.net

I want to rewrite the URL with query string. Here is an example
e.g
www.test.com/user.aspx?Name=1234
I want to rewrite like
www.test.com/1234
It is working fine with www.test.com?Name=1234 to www.test.com/test/1234
I am doing it like:
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RouteTable.Routes.MapPageRoute("StoreRoute",
"{Name}",
"~/Webpages/Test/Demo.aspx");
}

public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
protected void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("UserRoute", "{Name}", "~/user.aspx");
}
protected void Application_End(object sender, EventArgs e)
{
}
}
To access the value, use var v = Page.RouteData.Values["Name"];
A user navigating to www.test.com/1234 will be handled by www.test.com/user.aspx and the number 1234 will be passed through and accessed using the code snippet above.

If I am not mistaken, your problem is that you need to request a URL like www.test.com/1234 instead of www.test.com/test/1234.
This can be done using the route which you have mentioned in the global.asax file. But the issue here is that you have directly used a single dynamic parameter {Name} while defining your route. If you want to define any other route with a single parameter, then it will not work as explained below:
RouteTable.Routes.MapPageRoute("StoreRoute","{Name}","~/Webpages/Test/Demo.aspx");
RouteTable.Routes.MapPageRoute("StoreRoute1","{Name1}","~/Webpages/Test/Demo1.aspx");
In the above case, the second route will be overriden by the first route declared.
That is the reason, it's better to give a static parameter in the route declaration.
RouteTable.Routes.MapPageRoute("StoreRoute","test/{Name}","~/Webpages/Test/Demo.aspx");
RouteTable.Routes.MapPageRoute("StoreRoute1","test1/{Name1}","~/Webpages/Test/Demo1.aspx");
In the later case, the second route will not be overriden.
Now, if you only have to define a single route, then your code will work.
You can check my blog series on URL routing at below link. This link is of my resent post.
http://karmic-development.blogspot.in/2013/10/url-routing-in-aspnet-web-forms-same.html
Thanks & Regards,
Munjal

Related

Web.config in sub directory doesn't work when using page routes

I have an ASP.NET WebForms application with something along the lines of this file structure:
root\
default.aspx
web.config
subfolder\
page.aspx
web.config
If I access page.aspx by going to locahost/subfolder/page.aspx it reads the web.config in the subfolder just fine.
However, I have a route to the page setup like so:
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("", "test", "~/subfolder/page.aspx");
}
And when I try to access the page via that route, by going to localhost/test, the page loads just fine but it fails to read the values from the web.config in the sub folder.
Am I missing something? Is there some other step to allow a sub web.config to work with routes?
I'm accessing the sub web.config using:
var test = WebConfigurationManager.AppSettings["testSetting"];
I've been able to solve my issue by adding the following to my Global.asax:
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpRequest request = HttpContext.Current.Request;
Route route = RouteTable.Routes.Where(x => (x as Route)?.Url == request.Url.AbsolutePath.TrimStart('/')).FirstOrDefault() as Route;
if (route != null)
{
if (route.RouteHandler.GetType() == typeof(PageRouteHandler))
{
HttpContext.Current.RewritePath(((PageRouteHandler)route.RouteHandler).VirtualPath, request.PathInfo, request.Url.Query.TrimStart('?'), false);
}
}
}
By doing this, I fake out the Url property of the Request object to use the "real" URL to the page for any request with a Url that matches an existing page route. This way, when WebConfigurationManager pulls up config (which it does by current virtual path), it pulls it up using the appropriate page.

Routing to a fixed URL in ASP.NET MVC

Suppose I am working in a project called sampleProject. When a particular URL suppose /example/action is entered how will it redirect to another URL i.e. www.facebook.com ?
This is the code I'm trying:
routes.MapRoute(
"catalogAnnualreport2013", //Route Display Name
"/catalog/annualreport2013",
RedirectResult ("www.facebook.com")
);
In the global.asax file, you can simply do this:
void Application_Start(object sender, EventArgs e)
{
System.Web.Routing.RouteTable.Routes.MapPageRoute("YourReRoute", "example/action", "www.facebook.com");
}
You can use Redirect in your Action like that:
public ActionResult MyAction()
{
return Redirect("http://www.facebook.com");
}

problems by using routing asp.net

Hi to all i'm beginner in ASP.net ,i'd try using routing to introduce
some solutions by following code in global.asax:
protected void RoutingHandler(string routeName, string repUrl, string Url)
{
RouteTable.Routes.MapPageRoute(routeName,repUrl,Url);
}
protected void Application_Start(object sender, EventArgs e)
{
RoutingHandler("SolutionsRoute", "Solutions/{name}", "~/Pages/Solutions.aspx");
RoutingHandler("SolutionsPageRoute", "Solutions", "~/Pages/Solutions.aspx");
}
and it work good ,I'd use to link the page
in my menu but when i'm in the url if click again on the other link
the url cheng like this /Solutions/Solutions/VDI
how can I solve this problems
your problem is likely due to having two routing declarations for the same page. While this is valid it is not a recommended method. You should change your deceleration to instead specify a default null value like this
protected void RoutingHandlerWDefault(string routeName, string repUrl,
string Url, bool chkURL, string varOne)
{
RouteTable.Routes.MapPageRoute(routeName,repUrl,Url, chkURL,
chkUrl, new RouteValueDictionary { { varOne, string.Empty } });
}
protected void Application_Start(object sender, EventArgs e)
{
RoutingHandlerWDefault("SolutionsRoute", "Solutions/{name}", "~/Pages/Solutions.aspx",
false, "name");
}
SO Reference: asp.net webforms routing: optional parameters
Also note that you may want to change how you link to your urls. you can find references on how to link to routed urls in these links.
http://msdn.microsoft.com/en-us/library/cc668176.aspx
https://web.archive.org/web/20211020111718/https://www.4guysfromrolla.com/articles/012710-1.aspx
My problems was at the href's link that was like this solutions/security i'd placed a forwad slash behind of the url like this /solutions/security

Ajax autocomplete not working with routing

I have registered 4 routes in global.asax file its working fine but when i have added another route then ajax autocomplete suggestion list not displaying.
routing code is as below.
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
RouteTable.Routes.Add(new System.Web.Routing.Route("{resource}.axd/{*pathInfo}", new System.Web.Routing.StopRoutingHandler()));
RouteTable.Routes.MapPageRoute("StoreRoute", "{Name}", "~/Default.aspx");
RouteTable.Routes.MapPageRoute("DetailsView", "view/{id}/{popid}", "~/frmListingDetails.aspx");
RouteTable.Routes.MapPageRoute("Listing", "{keyword}/{city}/{area}", "~/Listing.aspx");
//RouteTable.Routes.MapPageRoute("Edit", "{id}/{vcode}", "~/Registration.aspx");
// RouteTable.Routes.MapPageRoute("Regp2", "Upload/{regid}/{ecode}", "~/RegPart2.aspx");
}
it is working fine but when i uncomment the commented root then ajax auto complete suggestion list not displaying
Add this line
routes.Ignore("{resource}.axd/{*pathInfo}");
to RegisterRoutes function.
By adding this ignore statement, you allow WebResource.axd to run properly.

Determining what Url the user originally entered to visit a web page after redirection

Currently, I'm just using clientside Javascript (location.href), but I am wondering if there is a way in Asp.Net to figure out the URL the user originally entered (assume I did not change it myself via 301), or at least to track it in a simple and reliable manner. As I am using my own implementation of URL rewriting via the global.asax (e.g. Context.RewritePath), this is not an easy task, particularly since I don't want to touch it too much.
Example
Global.asax:
public override void Init()
{
base.Init();
this.BeginRequest += new EventHandler(Global_BeginRequest);
}
void Global_BeginRequest(object sender, EventArgs e)
{
if (VARIOUSCONDITIONS) Context.RewritePath("SOMEURL");
}
SomePage.aspx.cs
protected void Page_Init(object sender, EventArgs e)
{
//Request.RawUrl is equal to "SOMEURL", as
//are other properties that store the URL.
}
Maybe I am misunderstanding your question, but if you are trying to capture the page the user first hits on your website, cant you capture this in the session_start event of global.asax? Then store in sessionstate or database for future use?

Resources