既然你说,你不能使用MvcController或Razor视图,你可以得到更接近金属:IHttpHandler
。
所以写一个开始:
public class MyHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
var response = context.Response;
response.ContentType = "text/javascript";
var message = "This is some super dynamic message. The UTC time now is: " + DateTime.UtcNow.ToLongTimeString();
var js = string.Format("alert({0});", new JavaScriptSerializer().Serialize(message));
response.Write(js);
}
public bool IsReusable
{
get { return true; }
}
}
然后写一个相应的路由处理:
public class MyHandlerProvider: IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return new MyHandler();
}
}
,并在您Application_Start
添加路由:
protected void Application_Start()
{
RouteTable.Routes.Add(
new Route(
"eventAggregation/events",
new MyHandlerProvider()
)
);
}
如果你没不想手动在您的Application_Start
中添加路线,您可以使用WebActivator
。简单地写一个静态的启动类:
internal static class Startup
{
public static void Application_Start()
{
RouteTable.Routes.Add(
new Route(
"eventAggregation/events",
new MyHandlerProvider()
)
);
}
}
,然后使用装配宽属性:
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(MvcApplication1.Startup), "Application_Start")]
而现在,所有剩下的在你看来是引用它:
<script type="text/javascript" src="~/eventAggregation/events"></script>
我我从来没有使用它,但从我的理解,'Node.js'用于此。 – 2013-05-14 01:15:18
您希望我将后端更改为Node.js? – Anders 2013-05-15 08:09:45