Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

.net - What clever things have you done with an ASP.NET MVC Action method

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.

Here's my contribution :

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"
        };
    }

Map a route to it :

  // 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>

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!

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

* 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Does a HTTP 301 Redirect count as clever?

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;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...