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.
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.
I have been using using System.Web.Routing for url routing in global.asax file code is as below.
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("Home", "", "~/Home.aspx");
routes.MapPageRoute("Posts", "Posts/{blog_url}", "~/blog-description.aspx");
}
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes( RouteTable.Routes);
}
Not quite sure what you are asking, but think you want the route debugging tool, which outputs which route is currently being selected: http://www.allenconway.net/2013/01/route-debugging-in-aspnet-mvc.html
I have an empty ASP.NET Web application set up to serve a NuGet repository to our team. I created it as an empty web application and didn't do much else except add the NuGet.Server package to the solution and add a test package to the Packages directory.
I've published the application to IIS, verified that I'm targeting the right framework (4.0), and looked through all the posts I can find on the topic. I haven't been able to find a fix. Basically, when I go to http://locoalhost/NuGet_Test, I expect to get redirected to http://localhost/NuGet_Test/Default.aspx, but it doesn't happen. I can go to http://localhost/NuGet_Test/Default.aspx directly and everything displays fine. But I don't know why it's not going to the default. I'm concerned because I don't know what to do if this happens with other web applications in the future.
Also, I have verified that default.aspx is in the Default Documents list on IIS (7.0).
better yet, try this..
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("", "/NuGet_Test","~/Default.aspx");
}
UPDATE: This was the actual solution tmoore used, no extra method & slight change in the URL form...thanks tmoore
protected void Application_Start(object sender, EventArgs e)
{
var routeCollection = RouteTable.Routes; routeCollection.MapPageRoute("DefaultRoute", string.Empty, "~/Default.aspx");
}
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