MVC3 默认返回视图
基本上,我想知道是否有人知道一种可以设置 MVC3 的方法,它首先查找操作,如果不存在,它将自动返回该位置的视图。否则每次我制作一个页面时,我都必须在添加操作后重建它。
这不会阻止项目工作,也不是问题,将其包含在代码中以帮助提高测试速度将是一件非常好的事情。
编辑:
只是为了清楚起见,这就是我每次创建一个内部没有任何逻辑的视图时所做的事情:
public ActionResult ActionX()
{
return View();
}
有时我会想要在操作内部有一些逻辑,但大多数时候对于空白页面我只想要上面的代码。
我希望有任何方法可以始终为每个控制器/操作组合返回上述代码,除非我已经执行了操作,否则它应该使用我指定的操作。
谢谢,
杰克
Basically, I was wondering if anyone knows of a way that you can set up MVC3 in a way that it will first look for an action, and if none exists, it will automatically return the view at that location. Otherwise each time I make a page, I will have to rebuild it after adding the action.
It isn't something that's stopping the project from working nor is it an issue, it would just be a very nice thing to include in the code to help with speed of testing more than anything.
EDIT:
Just for clarity purposes, this is what I do every time I create a view that doesn't have any logic inside it:
public ActionResult ActionX()
{
return View();
}
Sometimes I will want some logic inside the action, but majority of the time for blank pages I will just want the above code.
I would like it if there was any way to always return the above code for every Controller/Action combination, UNLESS I have already made an action, then it should use the Action that I have specified.
Thanks,
Jake
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不为此创建一个单独的操作呢?这将查找具有指定名称的视图,如果不存在则返回 404。
然后使您的默认路由回退到此:
因此,对 http://yoursite.com/somepage 的请求将调用页面(“某个页面”)
Why not just create a single action for this. This will look for a view with the specified name and return a 404 if it doesn't exist.
Then make your default route fall back to this:
So a request to http://yoursite.com/somepage will invoke Page("somepage")
我不完全确定这会有多大用处(或者它是否真的是一个好主意),但我想如果你有纯静态内容的页面(但可能使用布局或其他东西,所以你不能使用静态 html)可能有用
无论如何,这就是它可以完成的方式(作为基类,但不一定如此)
I'm not altogether sure how useful this will be (or whether its really a good idea) but I guess if you have pages which are purely static content (but maybe use a layout or something so you can't use static html) it could be useful
This is how it could be done though anyway (as a base class, but it doesn't have to be)