Is there any way that I can pass parameters to a user control/page through the path(URL) in a ASP.Net Web application(not MVC).
i.e.
http://abc.com/news/para-1/para-2
where para-1 and para-2 are parameters.
What you are looking for is called Routing.
If you're using .NET 4+, you can read how to implement it in a Web Forms application on MSDN.
Your rule essentially comes down to this, assuming news.aspx is where you want to go:
routes.MapPageRoute("NewsRoute",
"News/{arg1}/{arg2}",
"~/news.aspx");
You can then proceed to access the values using any of the following methods:
Page.RouteData.Values["arg1"]
<asp:Literal ID="Literal" Text="<%$RouteValue:arg1%>" runat="server"></asp:Literal>
If you're not using .NET 4+, Scott Hanselman writes about ASP.NET FriendlyUrls, which is available in NuGet.
You can use ASP.NET Friendly URLs
The ASP.NET Friendly URLs library makes it easy to enable
extensionless URLs for file-based handlers (e.g. ASPX, ASHX) in
ASP.NET applications.
There is a good introduction by Scott Hanselman: Introducing ASP.NET FriendlyUrls - cleaner URLs, easier Routing, and Mobile Views for ASP.NET Web Forms
You could try using QueryStrings.
Redirect your page like this:
Response.Redirect("Webform2.aspx?Name=" +
this.txtName.Text + "&LastName=" +
this.txtLastName.Text);
And on your new page get the values like this:
private void Page_Load(object sender, System.EventArgs e)
{
this.txtBox1.Text = Request.QueryString["Name"];
this.txtBox2.Text = Request.QueryString["LastName"];
}
Related
I've a site, we are using ASP.NET 4.0, and right now our products content is managed like this
www.franko1.com/products.aspx?serie=2000
where the querystring serie is the product ID, so its value is taken and then the contents is extracted from the database.
Now for SEO reasons, we've been asked to change the urls, so now they have to look like this:
What the boss want | Current Urls
--------------------------------------------------------------------------------
www.franko1.com/Relief_Valves | www.franko1.com/products.aspx?serie=2000
www.franko1.com/Inline_Flame_Arrester | www.franko1.com/products.aspx?serie=1000
www.franko1.com/Vent_Hatch | www.franko1.com/products.aspx?serie=3000
and so on ...
Right now, we are using a masterpage and the products.aspx and as I said, we take the querystring serie and we show the content based on its value, I have no idea how to do this using asp.net, I have read about ISAPI_Rewrite but I was wondering if there is a technique to approach this without dealing with the IIS server....
Well I don't know if I was clear, It is hard to explain,
No need for that. You can achieve this via routing (It's not just for MVC).
Routing has been available as a stand alone module for a while now, but with ASP.Net 4.0 you can now use routing for WebForms just as easily as you can with MVC.
You will need to add some routing to your Global.asax
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routeCollection)
{
routeCollection.MapPageRoute("Products", "Products/{Name}", "~/Products.aspx");
}
}
And with that you can now reference the route values in your page like so:
protected void Page_Load(object sender, EventArgs e)
{
string prodName = Page.RouteData.Values["Name"].ToString();
//Do lookup, etc...
}
Your URLs will end up looking like this:
www.domain.com/products/Relief_Valves
www.domain.com/products/Widgets
www.domain.com/products/TrashCans
etc..
Nice and easy... and clean!
URL rewriting was made just for exactly this purpose. The problem is that it really doesn't work with variable content directly appended to the root URL. There really isn't enough information for URL routing to separate such URLs from the rest of your URLs.
Would your boss accept www.franko1.com/p/Relief_Valves ?
i want to route Default.aspx to another URL when page starts.
my global.asax is like this :
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute(
"Default", // Route name
"My Site", // URL with parameters
"~/Default.aspx" // Parameter defaults
);
}
should i write a handler for my purpose?
(i found some samples for .net 3.5 and MVC but what about .net 4 web forms)
if yes how can i write it?
EDIT:
what this line exactly do?
routes.Add("Default", new Route(string.Empty, new RouteHandler("~/Default.aspx")));
i am using web forms -> Not MVC
thanks in advace
Here's a specific example of how to deal with routing on asp.net 4.0 web forms (it's just under the mvc part).
http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx
The way you are approaching it is fine. You do have an error in the second parameter of your route. Well perhaps not an error, I dislike spaces in urls as they are actually the encoded spaces. Check out the guide.
Just noticed your edit.
Adding the route essentially creates a mapping between a url or url pattern (which you have as string.Empty which is a problem) and a handler which serves the request(You specify RouteHandler which I don't believe actually exists?). .net Provides a PageRouteHandler which allows you to specify which page responds to your request and deal with a couple other details like security defined on the physical structure of your site. Internally, MapPageRoute is simply calling routes.Add but using the PageRouteHandler.
I am trying to come up with idea how to implement msdn like localization based on address string
http://msdn.microsoft.com/en-us/library/system.string.aspx
http://msdn.microsoft.com/ru-ru/library/system.string.aspx
I tried to find various information about ASP.NET localization, but failed to find a description of strategy that can be used to get above result.
I am not very experienced with ASP.NET and would like to get some advice on how to implement something that will result in similar paths on my website(using best possible way with least code duplication for example).
I understand that I could duplicate both files in 2 folders. or 10 files in 10 folders if i got 10 cultures. But that sounds like not the best strategy. Is there some rewrite & parameter pass going on behind or ?
Update: I ended up implementing it the following way:
for my localizable pages I register routes to all current cultures ( in a generic method),
for example for help.aspx I register ru-ru/help/ and en-us/help/ routes, inside help.aspx (and other localizable pages, oop way) I analyze address string and retrieve desired language. After that I setup html contents matching the related culture provided in url.
Microsoft appear to be either rewriting the URLs, or taking advantage of custom routing in ASP.NET. They are using using the URL to determine which culture to select. They could have used a query string but the URLs would not look as good.
I'd suggest that you start by looking into ASP.NET MVC as it uses routing out of the box. Then modify the routes to match those used by MS above, before using them to apply a culture.
Where you will be hosting your application could have an impact on whether you can do similar things. If you are managing the whole server and not using shared hosting it will be easier to implement these things, especially URL rewriting. If you have to use ASP.NET 2.0 because you are in some aging corporate environment you will probably have an even more difficult time.
Valentin, try to use url routing:
1 - Add routing info to global.asax
void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapPageRoute("cultureCrossroad", "{culture}/Library/{article}", "~/library/ArticleHost.aspx");
}
2 - Create host page at ~/Library/ArticleHost.aspx
public partial class ArticleHost : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// ! Here we have access to url's parts and can redirect user for desired page via Server.Transfer method.
var response = string.Format("culture: {0}<br/> article: {1}", Page.RouteData.Values["culture"], Page.RouteData.Values["article"]);
Response.Write(response);
}
}
I am using a custom route handler for a webforms application. I am using routes to determine localization. ie:
if the url has es or fr in the route it will load either spanish or french resources.
for example:
www.someroute/es/checkstuff/checkstuff.aspx
will load:
www.someroute/checkstuff/checkstuff.aspx with the spanish resources.
I am configuring the custom routes in global.asax via:
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
foreach (var value in _customRoutes)
{
routes.Add(value.RouteName, new Route(value.Route, new CustomRouteHandler(value.ResolvedRoute)));
}
}
where _customroutes is a list of routes.
Is there a way to do this with some kind of pattern matching so I can avoid adding a specific route for each page in the application. While I know I could use a t4 template to generate the routes, I guess I am looking for a dynamic way to create the list
I discovered that it was simpler to use MapPageroute than route.Add. With MapPageRoute I was able to use wildcards and with two entries:
routes.MapPageRoute("Spanish", "es/{*page}", "~/{page}");
routes.MapPageRoute("Kreyol", "fr/{*page}", "~/{page}");
I was able to provide the required routing for Spanish and Kreole pages.
Thanks to all for your help.
I would handle the language-part through some plain old Rewrite with an HttpModule in the BeginReguest handler and let the Routing engine take care of the rest.
Remember that the Routing mechanism takes place far later than BeginRequest so you can safely determine the language, set the CultureInfo on your thread and rewrite the request url to no contain the language-part, and your Routing would never know about it.
I have an existing site that I'd like to convert to use routing, and after reading Scott Guthrie's post here, I built a working sample that works for most circumstances. However, since not all of the pages on the existing site match a particular pattern, I'll need to check against a database to determine which route (destination .aspx page) to use.
For example, most pages are like this:
http://www.mysite.com/people/person.html
This is fine - I can easily route these to the view_person.aspx page because of the 'people' directory.
But some pages are like this:
http://www.mysite.com/category_page.html
http://www.mysite.com/product_page.html
This necessitates checking the database to see whether to route to the view_category.aspx page or the view_product.aspx page. And this is where I'm stuck. Do I create an IRouteHandler that checks the database and returns the route? Or is there a better way? The only code I've found that kind of fits is the answer to this question.
Thanks in advance.
If you don't mind doing so, the cleanest solution is to:
http://www.mysite.com/pages/category_page.html
In ASP.NET MVC, this situation would be handled a little differently, by specifying a default controller and action method on the root route.
Your route handler doesn't check the database. It sends all the requests to a handler .aspx script. It's that script that checks the database.
My register route looks like...
private static void RegisterRoutes()
{
Route currRoute = new Route("{resource}.axd/{*pathInfo}",
new StopRoutingHandler());
RouteTable.Routes.Add( "IgnoreHandlers", currRoute);
currRoute = new Route("{urlname}",
new EPCRouteHandler("~/Default.aspx"));
currRoute.Defaults = new RouteValueDictionary {{"urlname", "index.html"}};
RouteTable.Routes.Add( "Default", currRoute);
}
The custom handler, which shouldn't be needed in ASP.Net 4.0, simply passes the urlname parameter to the responding script as a URL variable.
Now how often the responding script checks the database depends on how often the data in the database is changed. You can easily cache paths and invalidate the cache when the data is suppose to have changed for instance.
For anyone stuck in the same situation, I ended up adapting the code from this answer to check against a database and return the proper ASPX page.