I am implementing a system that has a file-folder system (users can have folders and each folder can have files or folders in it again). I want to build the Url according to the folders. lets say a user has a folder 'Folder1' and in it there is another folder 'Folder2'. When I am in Folder1 I want the Url to look like 'Home/Folder1' and when user clicks 'Folder2' in 'Folder1' Url should look like 'Home/Folder1/Folder2' and it can be able to go like this forever :)
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// THIS IS THE LINE I ADDED
routes.MapRoute(
"Home",
"{folderPath}",
new { controller = "Home", action = "Index", folderPath = UrlParameter.Optional }
);
// END OF LINE I ADDED
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = -1 }
);
}
}
I changed the RouteConfig file and now I am able to have 'Home/Folder1' and 'Home/Folder2' unfortunately that is not what I want :/
I hope I explained my problem clearly.if anyone can help me I would appreciate it. By the I am actually using MVC4.
Add a * to folderPath:
"{*folderPath}",
But that will make the second route useless.
Related
I don't find how I can create a "fake" subfolder in MVC4.
I want that an URL like
AREA/CONTROLLERNAME/FAKEFOLDER/ACTION/
goes to
AREA/CONTROLLERNAME/ACTION
Is it possibile? Any suggestions?
Thanks!
Have you tried using routing? For example assuming you have an Admin area:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/FAKEFOLDER/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
Now when you request /admin/home/fakefolder/index the Index action of HomeController within the Admin are will be executed.
You could add a MapRoute that expects an extra parameter in your route (fakefolder in the example below). Then, the routing occurs as usual, where the controller's action serves the page. Example:
routes.MapRoute(
name: "FakeFolder",
url: "{controller}/{fakefolder}/{action}",
defaults: new { controller = "home", action = "index", fakefolder = UrlParameter.Optional}
);
Notice that with this routing you can use any "folder name" since it is just a placeholder.
Url example:
myController/SomeFakeFolder/someAction will execute the action someAction in myController
Here is my code:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Admin", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Admin", action = "Index", id = UrlParameter.Optional }
);
}
for first link it works well if i go to:
localhost/song
localhost/date
etc. it opens all links under home controller.
But for second maproute:
localhost/admin
localhost/admin/index
- these link are not working? Can anyone please tell me what i am doing wrong?
First, your default route must be last in the list, not first.
Second, You have two default routes. There is no way for MVC to know which one to use, so it always chooses the first one that matches. Instead, your Url for the admin one should be "Admin/{action}/{id}"
Im working my way though an ASP.NET MVC tutorial and couldnt find the answer im looking for.
I understand that each controller class in the 'Controller' root folder is mapped to a Url, so:
****Controller Folder****
|- StoreController.cs
Maps to $url/Store
However, If I wish to creater a 'subfolder'
I.e. a Controller class located for $url/Store/Testing I cant seem to see how I go about it.
I tried deriving a class from StoreController.cs, but that didnt work.
URLs do not necessarily correspond to MVC application internal folder structure. You can use MVC routing tables to conceal the internal structure and redirect specific URLs to any controllers/actions you want. For example, you can create a TestingController.cs class in the Controllers folder and use this route in Global.asax:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Store-Testing", // Route name
"Store/Testing/{action}/{id}", // URL with parameters
new { controller = "Testing", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
In this case, a request to http://[domain]/Store/Testing will be handled by TestingController.
That url would with the default route point to an action called Testing, within the Store controller.
You can however create your own custom routes in your global.asax file.
I have a problem. I have a area in MVC 3 called Page that works as it should.
I just added a new Area called Media and now I get "the resource cannot be found" for that new area. I am going crazy, since it looks exactly like the PageArea that works.
Here is the MediaAreaRegistration.cs
public override string AreaName
{
get
{
return "Media";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Media_default",
"{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
Here is my global.asax
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
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 = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
I am trying to access via localhost/media/, but I am just getting "the resource cannot be found".
Any ideas?
Check the Namespace of the Controller;
In my case; the default route was:
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
defaults: new {controller = "Home", action = "Index", AreaName="Admin", id = UrlParameter.Optional },
namespaces: new[] { "MyApp.Admin.Controllers"}
);
But when I was create the controller, the MVC automatically set "MyApp.WebUI.Areas.Admin.Controllers" as the namespace of the new Controller; I Changed the namespace to what I defined in default route as "MyApp.Admin.Controllers" and application works fine.
Typically, when you create an area, you will get a somewhat different default route than what is in global.asax. For example, I created a Media area in an MVC3 project, and the default route looks like this:
context.MapRoute(
"Media_default",
"Media/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
Routes in areas are really no different than routes defined in your global asax, except that they look for controllers in a different namespace. Also, they are loaded before the routes in your global.asax. You can see this because in Application_Start, RegisterAllAreas is invoked before RegisterRoutes.
Typically, this is the URL schema for root controllers with routes defined in your global.asax:
base/ControllerAName/Action1Name
base/ControllerAName/Action2Name
base/ControllerBName/Action6Name
...and so on. This is the "convention" you get with MVC out of the box. Look closely, and you will see that this pattern matches the base route definition in your global asax:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index",
id = UrlParameter.Optional } // Parameter defaults
);
On the other hand, the convention when you use areas is that your "conventional" URL schema will look like this:
base/AreaName/ControllerAName/Action1Name
base/AreaName/ControllerAName/Action2Name
base/AreaName/ControllerBName/Action6Name
Notice the difference? This is why your default route definition in the area registration looks like this: "Media/{controller}/{action}/{id}"
With all of this said, there is nothing stopping you from deviating from the conventions. It sounds like you want to have an area named Media, and a URL base/media that goes to some action method on some controller in the area. If that is correct, try this -- remembering to put your more specific route before the default route generated by MVC:
context.MapRoute(null,
"media",
new { action = "Index", controller = "Media" }
);
context.MapRoute(
"Media_default",
"Media/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
This means that MVC will match base/media to the Index action method on the MediaController in your Media area, since that route is defined first.
Also, when you create a new area, don't change any namespaces. This will only cause you problems.
Another tip is to not give route names to your routes. Notice how I passed null as the first argument. This is considered good practice -- accessing routes by name can get very messy.
I suggest you try starting a new project, or creating a new area, and trying these suggestions. Grasping routes coming from webforms can be tricky, but once you get a handle on it, I think you will find it superior to the URL-TO-FILE mapping in webforms.
In my case, someone added routes.Clear() in RouteConfig.cs, before any area ever existed. But now I added an area, this was erasing all its routes.
Works:
http://localhost/ApplicationName/Home/Index
http://localhost/ApplicationName/
http://localhost/ApplicationName/AnotherController/
404 Error:
http://localhost/ApplicationName/Home
I have an Index view for my Home controller and an Index action.
Everything was working fine, then I refactored and changed the application name.
Now only the Home controller won't default to the Index view when the action is left out
Here's my RegisterRoutes in Global.asax:
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 = UrlParameter.Optional }
);
}
There was an empty folder in the application root named Home. The application was going there first for the view. Deleted it, problem solved.