ASP.NET Routing in Global.asax - asp.net

I'm trying to add a route in my web forms application by following this:
http://msdn.microsoft.com/en-us/library/cc668201.aspx#adding_routes_to_a_web_forms_application
I've added the route in my Global.asax file like so:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("", "/WebsiteName/{combinedPin}", "~/Default.aspx");
}
I then try to visit my website locally like this:
http:// localhost:12345/WebsiteName/test36u
But I get a resource cannot be found message so I don't think my route is correct. Can anybody see a problem with my code?
Any pointers would be much appreciated.
Thanks

You do not need to specify the name of your website as part of the route, try with this code:
routes.MapPageRoute("", "{combinedPin}", "~/Default.aspx");
With the above code, your link would look like:
http://localhost:12345/WebsiteName/test36u
If however your intention is that your users access your site using a segment named: WebsiteName then use:
routes.MapPageRoute("", "WebsiteName/{combinedPin}", "~/Default.aspx");
But in the precedent code your users will have to access your resource as follows: (probably not the expected result though)
http://localhost:12345/WebsiteName/WebsiteName/test36u

Related

ASP.net "{Controller}/" returning 403.14 error

I am having a curious issue with one of my projects in development. The issue is with links to a certain URL "//localhost:62168/Images/Index". I have buttons linking to that URL but when "//localhost:62168/Images/" is accessed it returns a HTTP Error 403.14 - Forbidden error. See the below Image:
localhost//Images/
Oddly enough though, when I enter the URL exactly ("localhost:62168/Images/Index") it loads the page properly. See the below Image:
localhost/Images/Index
I've done plenty of research online and I believe it may be an issue with routing so below I've added the code of my "RouteConfig.cs" file. Unfortunately, I am new to ASP.net and MVC and not knowledgeable on proper Routing procedures:
using System.Web.Mvc;
using System.Web.Routing;
namespace ReedHampton
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
I've also tried multiple other "solutions" presented in other online forums to no avail. These include:
Adding "directoryBrowse enabled="true" " to web.config
Running "C:\windows\Microsoft.NET\Framework64\v4.0.30319> aspnet_regiis.exe -i" in Command Prompt
Going through IIS Express and registering IIS Express and ASP.Net
I would greatly appreciate any help that anyone can provide!
IIS will attempt to short-circuit the request if it finds something on the filesystem that matches the URL. In other words, I'd assume you have an Images directory in your document root. Therefore, IIS will attempt to hit this directory, rather than pass the request on to the ASP.NET machinery. Since you've disabled directory browsing, you get a 403.
Long and short, you need to keep your ASP.NET MVC routes unique from what you have physically in your document root. You could change the name of the Images directory to something like img, or put it in a parent folder like the default of /Content/Images. Otherwise, you'll need to change your controller name or create a custom route to that controller not called /Images.

How to hide extension of my asp.net application

I have created a website in asp.net application. Which contains hundreds of pages for Insert Update Edit and Delete.
I need URL rewriting without doing too much of code. Please guide me.
My Application URL like.
http://domain/Users/index.aspx http://domain/Users/product.aspx
http://domain/Users/product.aspx?catid=1
http://domain/Users/product.aspx?typeid=1
http://domain/Users/product.aspx?editid=1&type=2
I want to hide the .aspx from whole application and also the URL doesn't show the .aspx page url if user open directly http://domain/Users/product.aspx?typeid=1.
Please let me know if any solution.
What you're looking for is FriendlyUrls which will enable you to achieve what you want. Firstly you will need to install the package which you can do by using NuGet Console
Install-Package Microsoft.AspNet.FriendlyUrls
Then in your RouteConfig Class you want something like below which should do the trick.
public static void RegisterRoutes(RouteCollection routes)
{
routes.EnableFriendlyUrls();
}
There's a handy article here which I reccomend you read.

url rewrite in asp.net for dynamially generated urls

I have few dinamically generated urls like http://localhost:35228/begineercontent?name=lanaguages&id=23, I used routing to hide .aspx extension and now i want to see above url like this http://localhost:35228/begineercontent/lanaguages/23 i tried few url rewrite methods from iis url rewrite tool nothing worked kindly please help me out
This can be done very easy. You need to change/add this code in your global.asax.
void registerroute(RouteCollection routes)
{
routes.MapPageRoute("begineercontent", "begineercontent/languages/{Id}", "~/begineercontent.aspx");
}
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
registerroute(RouteTable.Routes);
}
You also need to include the right assembly in global.asax by adding
using System.Web.Routing
In your begineercontent.aspx you need to add this code to the page load function.
var Id=( Page.RouteData.Values["Id"] as string);
A more detailled tutorial on how to work with routes can be found here:
http://msdn.microsoft.com/en-us/library/vstudio/cc668177%28v=vs.100%29.aspx

Creating Application/Virtual Directories in a site that POINT to that site to simulate multiple sites. Good idea?

We need a way to say
www.site.com/india
www.site.com/asia
www.site.com/usa
etc...
But we want all these requests to point back to www.site.com and not have to physically create the files and directories for every type of site... so if I just create a virtual directory (www.site.com/india) and point it to www.site.com... then I figure I can look at the URL and set some parameters/text/images accordingly to fill out the template...
Does this make sense? Or are there issues with web.configs ? OR a better way
I would encourage you to consider Routing, as demonstrated here: http://www.asp.net/mvc/tutorials/asp-net-mvc-routing-overview-cs
The given sample, repeated here so you can (hopefully) see the relevance, is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace MvcApplication1
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
}
By similar methods, you can merely extract the information you need
The first example was for MVC Routing, to do webforms routing you should look here http://msdn.microsoft.com/en-us/library/cc668202(VS.90).aspx and here's a (very limited) example:
public static void RegisterRoutes(RouteCollection routes)
{
routes.Add("BikeSaleRoute", new Route
(
"bikes/sale",
new CustomRouteHandler("~/Contoso/Products/Details.aspx")
));
}
Using a custom routehandler that they build in the code. Routehandlers are easy to use, once you get the hang of it. And they allow you more expressiveness.
I think you'll find that localizing the same site five ways without having different web.configs pointing you to your localization files will be difficult.
What I would do is have www.site.com's index wither automatically discover the client's location or ask them what version of the site they want. Then, refer them to a default.aspx under that site's virtual directory, that uses a common set of your custom web controls, localized according to the resource file referenced in that site's web.config. That way, all you'll need to copy is the site skeleton, which can be as little as the index.aspx.
If the site is mostly aspxs, this isn't going to work as well. In that case, I would do the website selection as before, but use a query string to keep track of client location and thus localization. That should still allow you to bring up the correct localization settings.
No, please promise me not to create a virtual directory for each country in the world. Checkout routing instead. And here are some other examples.
Consider using routing or even better MVC if you haven't written the sites already. You should definitely NOT be creating multiple virtual directories - it would be a nightmare to maintain

ASP.NET MVC thinks my virtual directory is a controller

I have a virtual directory under my MVC website in IIS called "Files". This directory is at the same level as my Views directory. When I link to a file from my MVC app to a file under my Files directory, I get the following error:
The controller for path
'/Files/Images/1c7f7eb8-5d66-4bca-a73a-4ba6340a7805.JPG'
was not found or does not implement
IController.
It thinks that my Files VD is a controller. How do I access my files like a normal VD without MVC interfering?
Thanks.
When registering routes, try to add the following Ignore rules.
public static void RegisterRoutes(RouteCollection routes)
{
/* Ignore static content, see
http://weblogs.asp.net/rashid/archive/2009/04/03/asp-net-mvc-best-practices-part-2.aspx
*/
routes.RouteExistingFiles = false;
routes.IgnoreRoute("Content/{*pathInfo}");
routes.IgnoreRoute("Scripts/{*pathInfo}");
routes.IgnoreRoute("Styles/{*pathInfo}");
routes.IgnoreRoute("{*favicon}",
new { favicon = #"(.*/)?favicon.([iI][cC][oO]|[gG][iI][fF])(/.*)?" });
//Ignore handlers and resources
routes.IgnoreRoute("{resource}.ashx/{*pathInfo}");
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// your routes go here
}
ASP.Net looks for the directory first and then tries to match a controller, so what you are doing should work. Are you sure the file with that name exists and is accessible?
I think you'll have to add a call to routes.Ignore() a static route in your Global.asax file so that .NET MVC knows to ignore the request:
RouteCollection.Ignore(String) - MSDN

Resources