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
Related
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");
}
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
I am facing an issue in ASP.Net URL Routing. Following is the Global.asax code:
public static void RegisterRoutes(RouteCollection routeCollection)
{
routeCollection.MapPageRoute("Project", "{dealname}/{city}/{projectname}/{projectid}", "~/projectpage.aspx");
routeCollection.MapPageRoute("Home", "home/{dealname}/{city}", "~/index1.aspx", true, new RouteValueDictionary { { "dealname", "property-for-sale" }, { "city", "Ahmedabad" } });
routeCollection.MapPageRoute("ProjectType", "result/{dealtype}/{searchstring}", "~/result.aspx");
}
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}
When I open URL of the site Route "Home" is perfectly working. But when Redirect to the Route "ProjectType" using Response.Redirect, the "home/" portion of the previous URL remains as a result, it remains on the same page and in the URL it's showing /home/result/{dealtype}/{searchstring} instead of /result/{dealtype}/{searchstring}.
Please guide me what is missing or what should be done to resolve this issue.
Thanks,
Munjal
I found out the solution. Instead of using Response.Redirect(), use Response.RedirectToRoute(). This function is specifically used when implementing URL Routing.
Reference Link: http://msdn.microsoft.com/en-us/library/dd992853.aspx
Thanks,
Munjal
Currently my product page URL is like
http://www.localhost:80/products/default.aspx?code=productCode
I want to access product page with
http://www.localhost:80/productCode
I have used HTTP module for this.
public class UrlRewritingModule : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
context.AuthorizeRequest += new EventHandler(context_AuthorizeRequest);
}
void context_AuthorizeRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
if (some condition)
{
context.RewritePath(url);
}
}
void context_PreRequestHandlerExecute(object sender, EventArgs e)
{
//We set back the original url on browser
HttpContext context = ((HttpApplication)sender).Context;
if (context.Items["originalUrl"] != null)
{
context.RewritePath((string)context.Items["originalUrl"]);
}
}
}
I have register it in web.config and it is working fine. But when I deploy it in IIS that session and application variables are not throwing null referent Exceptions.
Can anyone help me?
Edit: Do it require extra code to access session/ Application variable for rewritten URLs
?
Have you tried using HTTPContext.Current?
I was able to solve issue (accessing session and application variables in subsequent pages rewritten by custom handler) by adding runAllManagedModulesForAllRequests="true" attribute in modules in web.config.
How to use Application object in the web page?
I thought it should be something like Session object.
But when I use Application, it shows the Reference like
System.Net.Mime.MediaTypeNames.Application
Obviously, it's not the one I'm looking for.
Has it been discarded in .NET 4?
If yes, what should I use to replace the Application object.
Are you referring to this one
Page.Application Property
Gets the HttpApplicationState object for the current Web request.
<%
this.Application["test"] = "some value";
%>
inside a WebForm should work. And in the code behind it's the same story:
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Application["test"] = "some value";
}
}
The Application (HttpApplicationState) property is very much there.
Seems you have some references that are causing the confusion.
In your CS code on a Page you should be able to use it
this.Application["key"] = myObject;
It should work if you try to access it from an ASP.NET page. Application is a property of Page that returns the HttpApplicationState.
protected void Page_Load(object sender, EventArgs e)
{
if(Page.Application["Foo"] != null)
{
// ...
}
}
If you want to access it from a static context, you can use HttpContext.Current:
if (HttpContext.Current.Application["Foo"] != null){ }