MVC3 默认返回视图

发布于 2024-12-09 11:29:34 字数 416 浏览 1 评论 0原文

基本上,我想知道是否有人知道一种可以设置 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒芜了季节 2024-12-16 11:29:34

为什么不为此创建一个单独的操作呢?这将查找具有指定名称的视图,如果不存在则返回 404。

    [HttpGet]
    public ActionResult Page(string page)
    {
        ViewEngineResult result = ViewEngines.Engines.FindView(ControllerContext, page, null);

        if (result == null)
        {
            return HttpNotFound();
        }

        return View(page);
    }

然后使您的默认路由回退到此:

routes.MapRoute("", "{page}", new { controller = "Home", action = "Page" });

因此,对 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.

    [HttpGet]
    public ActionResult Page(string page)
    {
        ViewEngineResult result = ViewEngines.Engines.FindView(ControllerContext, page, null);

        if (result == null)
        {
            return HttpNotFound();
        }

        return View(page);
    }

Then make your default route fall back to this:

routes.MapRoute("", "{page}", new { controller = "Home", action = "Page" });

So a request to http://yoursite.com/somepage will invoke Page("somepage")

折戟 2024-12-16 11:29:34

我不完全确定这会有多大用处(或者它是否真的是一个好主意),但我想如果你有纯静态内容的页面(但可能使用布局或其他东西,所以你不能使用静态 html)可能有用

无论如何,这就是它可以完成的方式(作为基类,但不一定如此)

public abstract class BaseController : Controller
{
    public ActionResult Default()
    {
        return View();
    }

    protected override IActionInvoker CreateActionInvoker()
    {
        return new DefaultActionInvoker();
    }

    private class DefaultActionInvoker : ControllerActionInvoker
    {
        protected override ActionDescriptor FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, string actionName)
        {
            var actionDescriptor = base.FindAction(controllerContext, controllerDescriptor, actionName);

            if (actionDescriptor == null)
                actionDescriptor = base.FindAction(controllerContext, controllerDescriptor, "Default");

            return actionDescriptor;
        }
    }
}

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)

public abstract class BaseController : Controller
{
    public ActionResult Default()
    {
        return View();
    }

    protected override IActionInvoker CreateActionInvoker()
    {
        return new DefaultActionInvoker();
    }

    private class DefaultActionInvoker : ControllerActionInvoker
    {
        protected override ActionDescriptor FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, string actionName)
        {
            var actionDescriptor = base.FindAction(controllerContext, controllerDescriptor, actionName);

            if (actionDescriptor == null)
                actionDescriptor = base.FindAction(controllerContext, controllerDescriptor, "Default");

            return actionDescriptor;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文