简体   繁体   English

MVC3自定义动作结果

[英]MVC3 Custom ActionResult

A lot of my plain content is in the database, accessed by a custom CMS. 我的很多普通内容都在数据库中,可以通过自定义CMS访问。 Around the application I display simple "thank you" messages, etc. which consist of a controller action (simplified): 在应用程序周围,我显示了简单的“谢谢”消息等,其中包括控制器操作(简化):

public ActionResult DetailsUpdated()
{
    return View();
}

and my view: 和我的看法:

@Html.GetContent("DetailsUpdated")

I have quite a few of these and its quite annoying having a lot of view files with one-liners in. I want to be able to return that content as a View, I can do return ContentResult(ContentRepository.GetContent("KEY")); 我有很多这样的东西,而且很烦人,因为有很多视图文件,而且只有一层。我希望能够以View的形式返回该内容,我可以return ContentResult(ContentRepository.GetContent("KEY")); but this returns as plain-text and there is no master view rendered. 但这将以纯文本形式返回,并且没有呈现主视图。

So, basically, grab the content from the DB via ContentRepository.GetContent("KEY") (returns a string) and inject it into a master view, where RenderBody() is called. 因此,基本上,可以通过ContentRepository.GetContent("KEY") (返回一个字符串)从数据库中获取内容,并将其注入到一个主视图中,在该主视图中调用RenderBody()。 I'd like to have a custom ActionResult so I can just do: 我想要一个自定义的ActionResult,所以我可以这样做:

public ActionResult DetailsUpdated()
{
    return DbContentResult();
}

and then the DbContentResult ActionResult will find the content key relative to the action and controller name, go to the database and retrieve the content and display it within its master view, no physical file view needed. 然后DbContentResult ActionResult将找到与操作和控制器名称相关的内容密钥,进入数据库并检索内容并将其显示在其主视图中,而无需物理文件视图。 Is this possible? 这可能吗?

You may have one view file and refer to that view file from several actions: 您可能只有一个视图文件,并通过多个操作引用了该视图文件:

public class FooBarController : Controller { 

    public ViewResult Foo() { 

        return View("FooView", ContentRepository.GetContent("KEY"));
    }
}

In this case, you will be able to render the view whose path is ~/Views/Shared/FooView.cshtml (unless you override the default convention of course). 在这种情况下,您将能够渲染路径为〜/ Views / Shared / FooView.cshtml的视图 (除非您当然重写默认约定)。

Edit: 编辑:

As you indicated, you can make a custom ViewResult which does this for you: 如您所指出的,您可以创建一个自定义ViewResult来为您执行此操作:

public class DbContentResult : ViewResult {

    public DbContentResult() {

        this.ViewName = "FooView";
        this.ViewData.Model = "Foo Model";
    }
}

Usage: 用法:

public ActionResult Index() {

    return new DbContentResult();
}

Or even better, write an extension method for Controller class which integrates with DbContentResult : 甚至更好的是,为Controller类编写一个扩展方法,该方法与DbContentResult集成在一起:

public static class ControllerExtensions {

    public static ViewResult DbContentResult(this Controller controller) {

        return new DbContentResult();
    }
}

Usage: 用法:

public ActionResult Index() {

    return this.DbContentResult();
}

有关创建自定义操作结果的更多详细信息,请访问此处:-http: //www.professionals-helpdesk.com/2012/06/create-custom-actionresult-in-mvc-3.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM