MVC - 创建新视图而不向控制器添加操作
我正在使用 asp.net MVC 1.0 开发一个类似 CMS 的迷你应用程序(2.0 发布后我将升级它)。我想要的功能对于 CMS 来说非常重要,那就是管理员向其网站添加页面的能力。
本质上,如果管理员想要添加一个名为“链接”的页面,我希望他们能够这样做,而不必经历将操作添加到控制器并编译新程序集的任何麻烦。
我有一个解决方案的想法,我想知道社区的想法。
我认为我应该编写一个名为(为了论证起见,我们将其称为 UserGenerateGenericController)的类来扩展 Controller 类。在此类中,我将有一个操作读取参数并重定向到与传递给操作的参数相对应的视图。
我还必须编辑 Global.asax.cs 文件中的路由
因此, /UserGeneratorGenericController/Links
将执行相同的操作 /UserGenerateGenericController/News
将命中,但将根据需要显示视图。
你说什么?我对您对此方法的评论以及对其他方法的建议感兴趣。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你提出的就是要走的路
i think what you presented is the way to go
您想要获取页面标题,并为其创建一个唯一的 url slug,然后您希望能够根据 url slug 从数据库加载内容(使用 url slug 作为 id,而不是实际的数据库 id) )。
因此,您的路线将是 /Pages/{UrlSlug},示例将是 /Pages/Links。然后,PagesController 上的 Index 操作将提取 url slug(链接)并从存储中加载适当的内容,并在主布局内呈现内容。我认为您是沿着这些思路思考的,只需确保当用户添加页面时,您为其创建一个唯一的网址。用下划线替换空格,删除特殊字符等,以创建一个 url 安全密钥,用于在请求时加载页面信息。
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).
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.