使用Actionfilter

时间:2013-05-23 09:09:50

标签: asp.net-mvc action-filter

我从未使用过actionfilter所以我真的不知道如何使用它,我做过som研究,但我完全不了解它。但基本上我要创建一个新的控制器,我希望我的_Viewstart.cshtml使用这个控制器。问题是我不知道如何将此代码用于actionfilter,然后在_viewstart中调用此控制器。

我想在控制器中使用的代码是。

var TemplateIDSession = Session["TemplateID"];
if (TemplateIDSession != null)
{
int tempID = (int)TemplateIDSession;
var template = servicetemp.GetEntry(tempID);
var servicetemp = ServiceFactory.Instance.CreateTemplateService();

Response.ContentType = "text/css";
return RazorEngine.Razor.Parse(System.IO.File.ReadAllText(Server.MapPath("Content/Site.css")));

1 个答案:

答案 0 :(得分:0)

我认为你应该以不同的方式解决这个问题,方法是使用CSS Helper for CSS,如下所示:

public static class Helpers
{
    public static string OrganizationStyleTemplate(this HtmlHelper html)
    {
        TagBuilder tagBuilder = new TagBuilder("style");

        var templateIDSession = HttpContext.Current.Session["TemplateID"];

        if (TemplateIDSession != null)
        {
            // retrieve your css from the database and insert it into the tag
            tagBuilder.InnerHtml = dbObject.CSSYouveStored;
        }

        return tagBuilder.ToString(TagRenderMode.Normal);
    }
}

然后用法就是这样:

@Html.OrganizationStyleTemplate()

P.S。如果您需要分析用户是否在该方法中进行了身份验证,请使用:

HttpContext.Current.Request.IsAuthenticated