English 中文(简体)
MVC - Creating new Views without adding Actions to Controller
原标题:

I m working on a mini CMS-like application using asp.net MVC 1.0 (I will upgrade it once 2.0 is released). Once feature I want, that is pretty vital to a CMS, is the ability for an admin to add pages to their site.

Essentially, if the admin wants to add a page called "Links", I want them to be able to do so without having to go through any of the hassle of adding the action to the controller and compiling a new assembly.

I have an idea for a solution and I want to know what the community thinks.

I think that I should write a class called (for arguments sake let s call it UserGeneratedGenericController) that extends the Controller class. In this class, I will have a single Action that reads a parameter and redirects to the View that corresponds with the parameter passed to the action.

I will also have to edit the Routing in the Global.asax.cs file

therefore /UserGeneratedGenericController/Links will hit the same Action that /UserGeneratedGenericController/News will hit, but will display the views as desired.

What say you? I m interested in your comments on this approach and your suggestions to other approaches.

最佳回答

i think what you presented is the way to go

问题回答

You want to take the page title, and create a unique url slug for it, then you want to be able to load the content from the database based on the url slug (using the url slug as the id, rather than an actual database id).

public ActionResult Index(string UrlSlug) {
  // Get Content For Page {UrlSlug}
}

So your route would be /Pages/{UrlSlug} and a sample would be /Pages/Links. Then your Index action on your PagesController would pull off the url slug (Links) and load the appropriate content from storage, and render the content inside your master layout. I think you were thinking along these lines, just make sure when the user adds a page, you create a unique url for it. Replace spaces with underscores, remove special characters, etc to create a url safe key to use to load the page information when it is requested.





相关问题
Rails: Proxy Pass?

I ve got a web-app that I want to migrate to Rails, which is currently just plain HTML with an Apache proxy to another server, running a custom database/webserver that serves the site s dynamic ...

MVC routing is not handling one of my directories

I m using ASP.NET MVC with IIS 7.0. I ve got 404 errors hooked up fine through my Application_Error override. In addition to "Controllers", "Models", "Helpers" etc. I have a directory called Files ...

Ruby on Rails conditional routing

I have a site listing many jobs, but I also want each account to be able to access its jobs in one place. Thus, I am using these routes: map.resources :jobs map.resource :account, :has_many => :...

Clean URL s using OpenCart s router class

How do you write clean URL s in OpenCart using their built in Router class? Here is my .htaccess file: RewriteEngine On RewriteRule ^(system) - [F,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{...

subdomain routing to multiple controller issue

I have a site: www.mydomain.com where we have administrative controls hidden away from normal customer view. I would like to only access the administrative features under a subdomain such as admin....

Can controller names in RESTful routes be optional?

With a standard map.resource routing mechanics and several nested resources the resultant routes are unnecessarily long. Consider the following route: site.org/users/pavelshved/blogs/blogging-horror/...

Help with rails routes

Im having a little trouble setting up routes. I have a users controller/model/views set up restfully so users is set up to be a resource in my routes. I want to change that to be usuarios ...

热门标签