Asp.net 4.0 Routing , accessing querystring in Web Forms - asp.net

I have an existing Asp.net 4.0 web form application which has used Asp.net routing 4.0. Now, I have to add a route to my web form application. But i cant figure out how to access the querystring to my aspx page.
What i did is,
routes.MapPageRoute("Products","Products/","~/WebPages/Products.aspx?pid=4",false)
After adding that piece of code what i expected was that,
when i browse http:\localhost\mysite\Products i should be redirected to my Products.aspx page and there I should be able to access the Request.QueryString["pid"].
But I am not able to access the querystring.
I doubt whether this is allowed in Asp.net 4.0 Routing for Webforms. How can i fulfill my requirement?
Thanks,
M

Something like:
routes.MapPageRoute("ProductssRoute",
"Products/{*queryvalues}", "~/Products.aspx",
false,
new RouteValueDictionary
{ { "pid", #"\d{4}" }});
See http://msdn.microsoft.com/en-us/library/cc668177.aspx

Related

Remove ASP.NET MVC URL routing from project

This is a similar question to
"How To Disable ASP.NET MVC Url routing" which hasn't been answered (I don't think the responders understood why it was needed).
I have an existing application that uses AngularJS and MVC url routing to serve the templates. I want to remove the MVC url routing as it is redundant (see comment below. we are using webApi to return data via ajax so the views/controllers are not needed).
I have removed the call to RegisterRoutes in the Global.asax.cs. There doesn't seem to be anything in the Web.config apart from a reference to System.Web.Routing so I left it.
I am using IIS Express with Visual Studio 2012.
When the application runs I get a 403.14 Forbidden error. I have enabled directory browsing. I have set the start page in the project in Visual Studio but I get a 404 despite the file definitely being there.
I have tried creating a non-mvc website and copying the contents of the web.config to my application, but this didn't work.
if you mean by removing the .aspx on the end (www.something/home.aspx);
In C#, instead of using
response.redirect("~/home.aspx");
use
server.transfer("~/home.aspx");
AngularJs routing has nothing to do with ASP.Net MVC Routing.
Angular Routing:
Is used for deep-linking URLs to controllers and views (HTML
partials). It watches $location.url() and tries to map the path to an
existing route definition.
ASP.Net MVC Routing:
ASP.NET routing enables you to use URLs that do not have to map to
specific files in a Web site. Because the URL does not have to map to
a file, you can use URLs that are descriptive of the user's action and
therefore are more easily understood by users.
In essence, AngularJs routing deals with client-side routing (using the location Url), whilst ASP.Net MVC deals with mapping http Urls to the appropriate controller/action in the server-side.

asp.net route mapping for web pages

I need to implement route mapping in asp.net web application. there are many tutorials telling how to do it in mvc. but i need to do it in web pages.
I have done it successfully for parent domain. my website also handles fake domain(wild card dns) as well, I need to map routes for subdomains as well.
http://mysite.com/login is mapped to http://mysite.com/default.aspx
but , now i want to map http://login.mysite.com to http://login.mysite.com/login.aspx
and http://signup.mysite.com/ to http://signup.mysite.com/signup.aspx
any idea how to do it?
Are you sure you need routing for this? It seems your just want redirection which can be handled by IIS or web.config.
If you really need routing, there are plenty of articles on both MVC as well as web forms, just search. In MVC you do it using the RouteConfig.cs file and in web forms you do it via Global.asax.cs file (or some helper referenced from global.asax.cs). Basically in web forms you add your routes to the RouteTable.Routes collection.
The code will look something like this (untested):
routes.MapPageRoute("",
"login",
"~/default.aspx");
Here are a couple of links which can get you started on routing:
Walkthrough: Using ASP.NET Routing in a Web Forms Application
RouteTable.Routes Property

Asp.net url routing in application having more than one page

I am using URL routing to make links to users profiles in asp.net, its work but the rest pages of the application not work , did i have to define a route for all pages or i miss something in somewhere.
I don't work with asp.net mvc but with asp.net web forms.
For profile page i want the routing but there is pages i just wont to use a asp x link to it
but all the pages now have the same URL defined in the routes in the global file.
I write this problem before two days always i have a problem.
You can debug your routes with ASP.NET MVC Routing Debugger Visualizer
or take a look at the blog of Phil Haack

Use System.Web.Mvc in non MVC ASP.NET Web Forms Application

We are developing an ASP.NET Web Forms application with REST modelled URL, for which we are using Route tables in global.asax. However, recently we came over a problem where the WebResource.axd and ScriptResource.axd files were getting routed as well. We wanted to use the RouteCollection.IgnoreRoute function. However, since it in System.Web.Mvc, it would mean that we would need to reference that dll in a non MVC Web Forms application.
Is it safe for us to continue with the approach. If anyone has a better way to ignore routes from within the Web Forms application please do share. Please also note that the application has been extensively developed in Web Forms and moving to MVC at the current stage is not feasible.
For web forms you need to use System.Web.Routing
Put this is Global.asax
void RegisterRoutes(System.Web.Routing.RouteCollection routes)
{
routes.Ignore("{resource}.axd/{*pathInfo}");
routes.MapPageRoute(
"Home", // Route name
"WWWWWW", // Route URL
"~/Default.aspx" // Web page to handle route
);
}
Did you know you could safely run an ASP.NET Web Forms and MVC application combined? Here are some links:
http://www.hanselman.com/blog/IntegratingASPNETMVC3IntoExistingUpgradedASPNET4WebFormsApplications.aspx
http://www.packtpub.com/article/mixing-aspnet-webforms-and-aspnet-mvc
Also, you can just use URL routing in ASP.NET 4 applications, as Registered User pointed out above. Read more about it here: 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

How to call a classic ASP.NET Page from a MVC Action?

I am using StructureMap with ASP.NET MVC. I have a requirement where I need to show a ReportViewer and for that I am using a classic ASP.NET page. The problem is when I am trying to redirect from the action I am getting following issue:
The IControllerFactory 'StructureMapControllerFactory' did not return a controller for the name 'Reports.aspx'.
Can someone please guide me on how do I call a classic asp.net page from an action and get rid of this error?
You need to map a route for your classic page so that the MVC engine skips it.
If you are looking to redirect the request to a 'classic' ASP.NET web form from the controller action, you might call HttpContext.Response.Redirect("URL"). The routing engine by default will resolve files that exist on the disk. No need to map new routes.

Resources