display time in asp.net?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication3.Controllers
{
public class HomeController : Controller
{
[OutputCache(Duration=10)]
public string Index()
{
return DateTime.Now.ToString("T");
}
public ActionResult About()
{
return View();
}
}
}
I just try to display the time in asp.net mvc2. But开发者_运维技巧 when i run the above program it gives the below exception can you please tell me what is wrong?
Server Error in '/' Application. The directive or the configuration settings profile must specify the 'varyByParam' attribute. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: The directive or the configuration settings profile must specify the 'varyByParam' attribute.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
As the error message states you need to specify a VaryByParam attribute for the cache you are using:
[OutputCache(Duration = 10, VaryByParam = "none")]
public string Index()
{
return DateTime.Now.ToString("T");
}
Also controller actions normally should return ActionResults and not strings:
[OutputCache(Duration = 10, VaryByParam = "none")]
public ActionResult Index()
{
return Content(DateTime.Now.ToString("T"), "text/plain");
}
It is recommended that action methods return ActionResult
so I'd suggest you try this.
[OutputCache(Duration = 10, VaryByParam="none")]
public ActionResult Temp()
{
return Content(DateTime.Now.ToString("T"));
}
or if you just want a fix for your snippet, try this.
[OutputCache(Duration = 10, VaryByParam="none")]
public string Temp()
{
return DateTime.Now.ToString("T");
}
精彩评论