你有一个ASP.NET MVC操作方法做了什么聪明的事情 [英] What clever things have you done with an ASP.NET MVC Action method

查看:138
本文介绍了你有一个ASP.NET MVC操作方法做了什么聪明的事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET MVC控制器操作方法主要用于处理业务操作,但它可以用于其它更多的。

The ASP.NET MVC controller action methods are primarily used for handling 'business' operations but it can be used for lots more.

我认为这将是有趣的,看看有什么创意,有用的东西的人创造了动作,它们可以是实际的或有用的人。

I thought it would be fun to see what creative, useful things people have created actions for that may be practical or useful for others.

下面是我的贡献:

JavaScript文件连接符 - 来减少HTTP请求数:

Javascript file concatenator - to reduce number of http requests:

    [OutputCache(Duration = 5 * 60, VaryByParam="")]  // DONT USE "None" here *
    public ContentResult RenderJavascript(){

        StringBuilder js = new StringBuilder();
        StringWriter sw = new StringWriter(js);

        // load all my javascript files
        js.AppendLine(File.ReadAllText(Request.MapPath("~/Scripts/jquery.hoverIntent.minified.js")));
        js.AppendLine(File.ReadAllText(Request.MapPath("~/Scripts/jquery.corner.js")));
        js.AppendLine(File.ReadAllText(Request.MapPath("~/Scripts/rollingrazor.js")));

        return new ContentResult()
        {
            Content = js.ToString(),
            ContentType = "application/x-javascript"
        };
    }

该地图的路线吧:

  // javascript
  routes.MapRoute(
     "js-route",
     "dynamic/js",
     new { controller = "Application", action = "RenderJavascript" }
  );

这是你的母版页参考吧:

Refer to it from your master page :

    <script type="text/javascript" src="/dynamic/js"></script>

被警告我设置为输出缓存,所以如果你改变你的JS和刷新页面,您可能要禁用缓存!

Be warned I've set a cache for the output, so if you're changing your JS and refreshing the page you might want to disable the cache!

我仅仅指刚要回来,并找出如何gzip压缩了。

I jsut need to come back and figure out how to gzip it.

* 你不应该使用的VaryByParam =无,因为这会导致Vary标头进行发送,其中的使浏览器回过头去检查是否有新版本。如果你真的有改变你的js的内容,那么你的用户只是布莱恩不得不等待5分钟吧!

* You shouldn't use VaryByParam="None" because that causes the Vary header to be send, which causes the browser to go back and check for a new version. If you really have to change your js content then your users are just goin to have to wait 5 minutes for it!

推荐答案

难道一个 HTTP 301重定向算聪明吗?

public class PermanentRedirectResult : ActionResult
{
    public string Url { get; set; }

    public PermanentRedirectResult(string url)
    {
        if (string.IsNullOrEmpty(url))
        {
            throw new ArgumentException("url is null or empty", "url");
        }
        this.Url = url;
    } 

    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }
        context.HttpContext.Response.StatusCode = 301;
        context.HttpContext.Response.RedirectLocation = Url;
    }
}

这篇关于你有一个ASP.NET MVC操作方法做了什么聪明的事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆