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");
}
Related
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.
Asp.net allows us to register new custom routes
http://www.example.com/products.aspx?category=software
to call them like that : http://www.example.com/products/software
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("testroute", "Test/{parame}", "~/Default.aspx");
}
This method works in Kentico but only for .aspx pages (for example for pages in ~/CMSPages/Default.aspx).
My question is how to achive same results for pages created with Portal Template approach?
I was trying to make my own HttpHandler
public class CustomHandlerProduct : IHttpHandler
{
public CustomHandlerProduct()
{
//
// TODO: Add constructor logic here
//
}
public bool IsReusable
{
// To enable pooling, return true here.
// This keeps the handler in memory.
get { return false; }
}
public void ProcessRequest(HttpContext context)
{
}
}
or even HttpModule according to this article
link to CodeProject article
but i can not achive desired results
any Ideas?
If you want to use Portal engine, is there a reason not to use Kentico URL rewriting capabilities? Wildcard URLs work very similarly to routes. See the documentation https://docs.kentico.com/display/K82/Wildcard+URLs
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
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