I've built a number of ASP.Net MVC sites and in each of them there are a number of pages suited to MVC's Controllers and Actions, and a number of pages that are really just brochure pages - /why, /why/ouradvantage, /about, etc - pages that have no real functionality, just a View, maybe a Layout, and that's it.
For these brochure-style pages I'd really prefer to have just the View and a good Route to find it, so I could put the /why at /Brochure/Why.cshtml or /Brochure/Why/Index.cshtml and either way it will be picked up just fine. I'd like to avoid making silly Controllers and Actions (as I've done in the past) to handle this set of URLs and pages.
How can I go about this in an ASP.Net MVC project? This must be a common need.
EDIT: An example of how I can do this the verbose way:
I could use the standard MVC route ({controller}/{action}/{id}) and spam a bunch of useless Controllers to get the set of URLs and pages I want. Every time I want to add a brochure-style (no functionality, just a View) page I'd add a Controller or Action like this:
Why Controller:
public class WhyController : Controller
{
public ViewResult Index()
{
return View();
}
public ViewResult OurAdvantage()
{
return View();
}
}
This puts a View at /why and /why/ouradvantage - clean URLs. If I wanted an /about page, I could add another Controller that does nothing but return a View named AboutController. If it had 5 subpages I could add 5 Actions to that Controller, all of those Actions doing nothing but return a View.
If these brochure-style pages in the MVC site amounted to say, 100 pages, I'd have quite a few needless Controllers and Actions all doing nothing really. Not very DRY. I'm interested in a way to just put Views in a folder in my Project and have them accessible just for being there (Configuration through Convention), at clean URLs like /why and /why/ouradvantage.
There are a few ways that get me close:
I could put a bunch of .cshtml pages in and visit them directly - but then I have to have the file extension in the URL and the View files themselves have to sit in the root.
I could use ASP.Net Areas to define an Area for these, but then all brochure-style pages have to sit at least one URL segment deep and I still have the above problem of file extensions in URLs.
There are crazy Routes I can define.
I suspect this comes up often in MVC projects that have a small or large number of these Brochure-style pages - it seems like there should be a clean way to do this.
EDIT: A crappy solution that spams the routing engine.
Create a class that maps routes like:
public static void MapRoutes(RouteCollection routes, string appRoot, string path)
{
if (!path.Contains("~/"))
throw new NotSupportedException("Pages path must be virtual (use ~/ syntax).");
var physicalPath = appRoot + path.Substring(2).Replace("/", "\\");
var dir = new DirectoryInfo(physicalPath);
var pages = dir.GetFiles("*.cshtml", SearchOption.AllDirectories);
int rootLength = appRoot.Length;
var rootParsed = pages.Select(p => "~/" + p.FullName.Substring(rootLength).Replace("\\", "/"));
int folderPathLength = path.Length + 1;
var mapped = rootParsed.Select(p => new {
Url = p.Substring(folderPathLength, p.Length - 7 - folderPathLength),
File = p
});
var routedPages = mapped.Select(m => routes.MapRoute(
name: m.Url,
url: "{*url}",
defaults: new { path = m.File, Controller = "Brochure", Action = "Catchall" },
constraints: new { url = m.Url }
)).ToArray();
}
You can call this in RouteConfig like:
BrochureRoute.MapRoutes(routes, Server.MapPath("~/"), "~/Brochure");
That obviously maps all these pages to a BrochureController, which you'll need as well:
public class BrochureController : Controller
{
public ViewResult Catchall(string path)
{
return View(path);
}
}
2 problems:
It spams the routing engine as I mentioned - if you have 100 pages you have 100 routes.
Passing the path as above seems to upset the normal Razor pipeline - visiting a page in this manner gets you an error like:
The view at '~/Brochure/About.cshtml' must derive from WebViewPage
To map individual pages RouteCollection.MapPageRoute method can be used.
routes.MapPageRoute("", "why", "~/Brochure/Why.aspx");
Sure, you could use Razor view engine (.cshtml) instead of WebForms.
Check MSDN documentation for some more details.
Update 2
You are right you won't be able to use this for for .cshtml pages. However, you don't need to use routing to access Web Pages (.cshtml files). It is enough to create the files, and omit the extension in URLs. To achieve your desired structure you could do this:
Your web project must allow Web Pages rendering. To enable this go to web.config and set webpages.Enabled to true. (<add key="webpages:Enabled" value="true" />)
Create folder why in your web application root
Add MVC ViewPage named Index.cshtml. This will be accessible from http://yoursite.com/why
Add MVC ViewPage named ouradvantages.cshtml. This will be accessible from http://yoursite.com/why/ouradvantages
You will also be able to access url data using #UrlData collection
Check more about this here. More about Web Pages in general is also available on asp.net website.
If for some reason you really need to use routing, you'll need custom RouteHandler. You can find one implemented on Nuget. Usage examples of this package are here.
Update
If you'd like to avoid manually adding each route you have few choices.
1) Create a convention to identify brochure pages
You could do this by expecting URLs to be brochure pages by default, and isolating "non-brochure" pages to specific sections:
routes.MapPageRoute("Default", "{brochurepage}", "~/Brochure/{brochurepage}.aspx");
// isolate non-brochure pages to "site" section
routes.MapRoute(
"",
"site/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
2) Hold brochure page names in a collection
List<string> brochurePages = new List<string>()
{ "about", "why", "contact" }; // add more pages here
....
foreach (var brochurePage in brochurePages)
routes.MapPageRoute("", brochurePage, "~/Brochure/" + brochurePage + ".aspx");
I have to be very specific about two URLs you mentioned in your question. To implement routing for "/why" and "/why/ouradvantage" you will NOT be able to write elegant code. This will take special handling.
If your issue is that you don't want to have separate controllers for all these "silly" pages, may I suggest that you create a logical way to route them to a singular controller and then render the correct data/ view based on the route?
For instance, your URLs could read "/Pages/Why/About" and you could route that to your pages controller and it would receive the "Why" and "About" as parameters and you could render your content accordingly. Does that make sense?
Related
i've created a basic mvc3 website whereby each controller represents the first folder in a url structure.
for example, the "food" and "drinks" folders below are controller. there are only two controllers which contain all of the sub-items in them.
ie in the first line of the example, controller=food, method=asian
in the second line controller=food, method=pad-thai and so on and so forth.
www.mysite.com/food/asian/
www.mysite.com/food/asian/pad-thai
www.mysite.com/food/italian/chicken-parmigiana
www.mysite.com/drinks/cocktails/bloody-mary
how would i write routes so that www.mysite.com/food/asian/pad-thai will direct to the food controller and the paid thai method within that controller, and also have a rule to send from www.mysite.com/food/asian/ to the food controller and asian index method??
The MVC design pattern isn't for rewriting URLs to point to folder structures. It can do this but it certainly isn't its main purpose. If you're trying to create a URL structure with static content, it might be easier to use the URL rewriting functionality built into IIS.
If you're creating a full MVC application, set up FoodController and DrinkController to serve up your views, for example:
public class FoodController : Controller
{
public ActionResult ViewDishByTag(string itemType, string itemTag)
{
// If an itemType is displayed without itemTag, return an 'index' list of possible dishes...
// Alternatively, either return a "static" view of your page, e.g.
if (itemTag== "pad-thai")
return View("PadThai"); // where PadThai is a view in your shared views folder
// Alternatively, look up the dish information in a database and bind return it to the view
return ("RecipeView", myRepo.GetDishByTag(itemTag));
}
}
Using the example above, your route might look a little like this:
routes.MapRoute(
"myRoute",
"{controller}/{itemType}/{itemTag}",
new
{
controller = UrlParameter.Required,
action = "ViewDishByTag",
itemtype = UrlParameter.Optional,
itemTag = UrlParameter.Optional
}
);
Your question doesn't contain much detail about your implementation, so if you'd like me to expand on anything, please update your question.
There were questions about multilingual apps in MVC here on SO but they were mostly answered by giving details about implementing Resource files and then referencing those Resource strings in Views or Controller. This works fine for me in conjunction with the detection of user's locale.
What I want to do now is support localized routes. For instance, we have some core pages for each website like the Contact Us page.
What I want to achieve is this:
1) routes like this
/en/Contact-us (English route)
/sl/Kontakt (Slovenian route)
2) these two routes both have to go to the same controller and action and these will not be localized (they will be in English because they are hidden away from the user since they are part of the site's core implementation):
My thought is to make the Controller "Feedback" and Action "FeedbackForm"
3) FeedbackForm would be a View or View User control (and it would use references to strings in RESX files, as I said before, I already have set this up and it works)
4) I have a SetCulture attribute attached to BaseController which is the parent of all of my controllers and this attribute actually inherits FilterAttribute and implements IActionFilter - but what does it do? Well, it detects browser culture and sets that culture in a Session and in a cookie (forever) - this functionality is working fine too. It already effects the Views and View User Controls but at this time it does not effect routes.
5) at the top of the page I will give the user a chance to choose his language (sl|en). This decision must override 4). If a user arrives at our website and is detected as Slovenian and they decide to switch to English, this decision must become permanent. From this time on SetCulture attribute must somehow loose its effect.
6) After the switch, routes should immediately update - if the user was located at /sl/Kontakt
he should immediately be redirected to /en/Contact-us.
These are the constraints of the design I would like. Simply put, I do not want English routes while displaying localized content or vice-versa.
Suggestions are welcome.
EDIT:
There's some information and guidance here - Multi-lingual websites with ASP.NET MVC, but I would still like to hear more thoughts or practices on this problem.
Translating routes (ASP.NET MVC and Webforms)
How about this?
Create custom translate route class.
Localization with ASP.NET MVC using Routing
Preview:
For my site the URL schema should look
like this in general:
/{culture}/{site}
Imagine there is a page called FAQ,
which is available in different
languages. Here are some sample URLs
for these pages:
/en-US/FAQ /de-DE/FAQ /de-CH/FAQ
Why not create the action names desired and simply RedirectToAction for the single, real implementation?
public ActionResult Kontakt() {
return RedirectToAction("Contact");
}
public ActionResult Contact() {
return View();
}
I just used a simple solution with "Globalization Resources", like this:
routes.MapRoute(
"nameroute", // Route name
App_GlobalResources.Geral.Route_nameroute+"/{Obj}", // URL with parameters
new { controller = "Index", action = "Details", Obj = UrlParameter.Optional } // Parameter defaults
);
But, you could customize as needed.
In the conventional website a url displayed as:
http://www.mySite.com/Topics
would typically mean a page sits in a subfolder below root named 'Topics' and have a page named default.htm (or similar).
I'm trying to get my head in gear with the MVC way of doing things and understand just enough of routing to know i should be thinking of URLs differently.
So if i have a db-driven page that i'd typically script in a physical page located at /Topics/index.aspx - how does this look in an MVC app?
mny thx
--steve...
It sounds like you are used to breaking down your website in terms of resources(topics, users etc) to structure your site. This is good, because now you can more or less think in terms of controllers rather than folders.
Let's say you have a structure like this in WebForms ASP.NET.
-Topics
-index.aspx
-newtopic.aspx
-topicdetails.aspx
-Users
-index.aspx
-newuser.aspx
-userdetails.aspx
The structure in an MVC app will be pretty much the same from a users point of view, but instead of mapping a url to a folder, you map a url to a controller. Instead of the folder(resource) having files inside it, it has actions.
-TopicController
-index
-new
-details
-UserController
-index
-new
-details
Each one of these Actions will then decide what view (be this html, or json/xml) needs to be returned to the browser.
Actions can act differently depending on what HTTP verb they're repsonding to. For example;
public class UserController : Controller
{
public ActionResult Create()
{
return View(new User());
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(User user)
{
// code to validate /save user
if (notValid)
return new View(user);
else
return new View("UserCreatedConfirmation");
}
}
This is sort of a boiled down version of RESTful URLs, which I recommend you take a look at. They can help simplify the design of your application.
It looks just like you want it to be.
Routing enables URL to be quite virtual. In asp.net mvc it will end at specified controller action method which will decide what to do further (i.e. - it can return specified view wherever it's physical location is, it can return plain text, it can return something serialized in JSON/XML).
Here are some external links:
URL routing introduction by ScottGu
ASP.NET MVC tutorials by Stephan Walther
You would have an default view that is associated with an action on the Topics controller.
For example, a list page (list.aspx) with the other views that is tied to the list action of the Topics controller.
That is assuming the default routing engine rules, which you can change.
Read more here:
http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx
IMHO this is what you need for your routes.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Topics", action = "Index", id = "" } // Parameter defaults
);
You would need a TopicsController that you build the View (topics) on.
Are there any good examples of mvc routing wherein every 404 page not found request is routed to a standard view in MVC which basically pulls the content from the database.
Just add this route to the bottom of your RouteTable:
routes.MapRoute("DynamicPages", "{*page}", new { Controller = "DynamicPages", Action = "Show", Page = String.Empty });
And create a controller for displaying dynamic pages from db:
public class DynamicPagesController : Controller
{
public ActionResult Show(string page)
{
var pageContent = DB.GetContentForPage(page);
return Content(pageContent);
}
}
Here's one way to do this: In your global.asax file in Application_Start, you need to set the default controller factory. Override it with an instance of your own factory.
void Application_Start()
{
ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());
}
MyControllerFactory should inherit from DefaultControllerFactory and when selecting the controller to use, look in your database for the appropriate page you want to display. If the page exists, select the appropriate controller and override the action in the requestContext.RouteData collection to point at the appropriate action for displaying dynamic pages.
If the requested page doesn't exist, pass back a call to the base method and let it do what it would normally do.
There are other ways you could do it, but this one should work and allows you to intercept the request before you hit the 404 page.
modify the web.config file, you may Reference to this page and look at the setting custom error pages in web.config section.
In a content management system you can usually create pages on the fly eg
www.website.com.au/home.aspx
www.website.com.au/projects.aspx
www.website.com.au/contact-us.aspx
In a previous CMS that we wrote we physically created these files on disk when the user selected to create a new page in his site. We copied the new file from a base template page, renamed the file and renamed the class in the code behind eg
template_page.aspx and template_page.aspx.cs turned into
projects.aspx and projects.aspx.cs
This was all done via our CMS application. No files needed to be manually created by a user.
How would this approach work using MVC?
Eg www.website.com.au/home/
www.website.com.au/projects/
www.website.com.au/contact-us/
Presumably we would need to dynamically create controllers and views on the fly?
This seems even messier than the old approach but I suppose its feasible.
Can anyone think of a smarter way to do it?
You should be able to use one controller and a couple views (display, create, edit) with some routing work. I did a super simple implementation for a personal project that went like this. I put this route near the top of my routing list and used the constraint to determine if it should be considered as a static page from my rules. My implementation didn't have any sort of hierarchy, i.e. pages/About-us/contact - only /contact.
route:
routes.MapRoute("StaticContent", "{title}",
new { controller = "Page", action = "Details"},
new { title = new InvalidTitleContstraint()});
controller:
public class PageController : Controller
{
// Details checks if it can find a matching title in the DB
// redirects to Create if no match
public ActionResult Details(string title)
// GET
public ActionResult Create()
// POST
public ActionResult Create(Page page)
}