ASP.NET MVC Routing config issue - c#

I have an application into application pool of IIS 7. I added a new virtual directory into the application. My application run on http://localhost.com/ and I would like run my virtual directory on http://localhost.com/VirtualDirectory/. The reason why i would like to do that is the virtual directory is used as application. I would like to use Virtual Directory as SubSite. My problem is that i can't access controllers and views under virtual directory. I access it when i change the return result of ActionResult method as "return View("/VirtualDirectory/Views/Home/Index.cshtml");". I don't wanna hard code the VirtualDirectory on every return Action result.
My Route config is:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Home", // Route name
url: "{id}", // URL with parameters
defaults: new { controller = "Home", action = "Index" /*, id = ""*/ } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Defaut", id = UrlParameter.Optional } // Parameter defaults
);
}
I try to make the next change:
routes.MapRoute(
"Default", // Route name
"VirtualDirectory/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Defaut", id = UrlParameter.Optional } // Parameter defaults
);
But when i try to load http://localhost.com/VirtualDirectory I've got the message:
HTTP Error 403.14 - Forbidden
The Web server is configured to not list the contents of this directory.

You shouldnt need to change anything at all in the routing of your application to run in a virtual directory.
However you will still need to make sure you use relative paths (not absolute / based paths) throughout your application. This is the same with any technology. / always ignores the virtual directory.

Related

Dynamic Routing in ASP.NET MVC

I've used this routing in localhost and it works fine:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Authentication", action = "Login", id = UrlParameter.Optional }
);
But when I deploy to a server with this structure:
servername/folder/Controller/Action
the styles and the pages get 404 error.
I want to change the routing dynamically between localhost and server
Stylesheets do not use routing they are in a set path that you set in the _layout If you want to set them dynamically you can use your web.config and do a transform to set the location based on the environment you are deploying to.
If you want to do dynamic routing then you need a placeholder, and you will need to supply the value at runtime. The files must exist in the physical path that matches the route
routes.MapRoute(
name: "Default",
url: "{servername}/{folder}/{controller}/{action}/{id}",
defaults: new { servername = "servername", folder = "foldername",
controller = "authentication", action = "Login", id = UrlParameter.Optional }
};

How to route Mvc Controller action when there is subdirectory?

This type of url works: ABC/CSDS?id=314
Folder Structure:
Controllers - Folder
,ABCController - Class
,CSDS = Action
Global.cs file has:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "ABC", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
How to make change for subdirectory url like
ABC/TESTN/CSDS?id=314 to go to
Controllers - Folder
,ABCController - Class
,CSDS = Action
Currently it says "The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable"
How about adding a rule like this:
routes.MapRoute(
"Default", // Route name
"{controller}/TESTN/{action}/{id}", // URL with parameters
new { controller = "ABC", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Warning, not tested, I don't have the tools installed at work :(

Asp.Net Routing - Display complete URL

I have a domain "http://www.abc.com". I have deployed an ASP.net MVC4 app on this domain. I have also configured a default route in RouteConfig.cs as shown below
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "MyApp", action = "Home", id = UrlParameter.Optional }
);
The above mapping ensures that anyone attempting to visit "http://www.abc.com" is automatically shown the page for "http://www.abc.com/MyApp/Home"
Everything works as expected but the address bar in the browser shows "http://www.abc.com" instead of "http://www.abc.com/MyApp/Home". Is there any way to force the browser to show the complete URL including the controller and Action?
One option would be to set your default route to a new controller, maybe called BaseController with an action Root:
public class BaseController : Controller
{
public ActionResult Root()
{
return RedirectToAction("Home","MyApp");
}
}
and modify your RouteConfig to point to that for root requests:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Base", action = "Root", id = UrlParameter.Optional }
);
You'll need to do some kind of url rewriting. Probably the quickest way is to add a RewritePath call to your BeginRequest in Global.asax. In your case it'd be something like this:
void Application_BeginRequest(Object sender, EventArgs e)
{
string originalPath = HttpContext.Current.Request.Path.ToLower();
if (originalPath == "/") //Or whatever is equal to the blank path
Context.RewritePath("/MyApp/Home");
}
An improvement would be to dynamically pull the url from the route table for the replacement. Or you could use Microsoft URL Rewrite, but that's more complicated IMO.
Just remove the default parameters, it's been answered here:
How to force MVC to route to Home/Index instead of root?

MVC routing ambiguity

I have a psychical path: Home/signUp.aspx
and Controller/HomeController.cs (related to Views/home/index.aspx)
My routing is configured in the global.asax:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.IgnoreRoute("{resource}.ashx/{*pathInfo}");
//settings - manage toolbars
routes.MapRoute("SettingsManageToolbars", "home/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
"Default",
// Route name
"toolbar/apps/{controller}/{action}/{id}",
// URL with parameters
new { controller = "Home", action = "Index", id = "" }
// Parameter defaults
);
}
When I surf to home\signup.aspx
I get a text page with "directory content"
How can I fix this routing?
By default, routing does not handle requests that map to an existing physical file on the Web server. Routing does not handle the request even if it matches a defined pattern, such as {controller}/{action}/{id}.
If you want routing to handle all requests, even requests that point to files, you can override the default behavior by setting the RouteExistingFiles property of the RouteCollection object to true. When you set this value to true, all requests that match a defined pattern are handled by routing.
http://msdn.microsoft.com/en-us/library/cc668201.aspx#scenarios_when_routing_is_not_applied
Why is your home controller an aspx file? Try making it a HomeController.cs!
I'd suggest deleting your current controller and adding another via the context menu (right-click, Add)

MVC Routing problem

I am learning MVC and I need to understand why it doesn't work the way it should.
Here is my routing :
public static void RegisterRoutes(RouteCollection routes)
{
// Note: Change the URL to "{controller}.mvc/{action}/{id}" to enable
// automatic support on IIS6 and IIS7 classic mode
//http://localhost/store/category/subcategory/product
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Category", // Route name
"store/{category}/{subcategory}", // URL with parameters
new
{
controller = "Catalog",
action = "Index",
category = "Featured Items",
subcategory = "All Items"
}
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" }, // Parameter defaults
new { controller = #"[^\.]*" } // Parameter constraints
);
}
The way I understand routing I should see the following url when I start the web app :
http:/localhost/store/
What I get is the second route....
Furthermore if I change the second route to "home/{action}/{id} it doesn't catch any routes.
Could you help me understand this please..Thanks
Routes do not specify default URL; the default URL is handled by your app. Routing specifies that when it sees http://localhost/store/bikes/mountain, it will use the catalog controller. But that doesn't specify the default URL; you have to enter that in the project properties.
I would recommend not changing the second one because unless you are creating groupings for all of your controllers, it's best to have the default as it is so you can catch all URL's. Your change to the second one would require the url to be:
http://localhost/home/home/index to match the HomeController's index action, whereas the default setup catches http://localhost/home/index...
Does that make sense?
Try this: http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx

Categories

Resources