2012-01-07 93 views
0

我需要在现有的asp.net应用程序中进行路由 - 而不是一个asp.net mvc(是的,我知道我应该转换,但让我们说现在是不可能的不要告诉我:)) - 我该怎么做路由到正常类,而不是一个aspx页面,因为所有的示例代码我看到的是总是与aspx页面喜欢这里:纯粹的ASP.NET路由到一个类而不是aspx页面

http://msdn.microsoft.com/en-us/magazine/dd347546.aspx

精确,我想要做一点MVC控制器路由:控制器例如产品是一个纯类,您可以通过访问http://domain.com/product

+0

要上课吗?就像在App_Code/YourClass.cs中一样? – justinlabenne 2012-01-07 21:39:31

+0

不,我想在MVC控制器路由中这样做:例如产品的控制器是一个纯粹的类,您可以通过http://domain.com/product – user310291 2012-01-07 22:17:33

+0

访问什么是纯类?控制器从System.Web.Mvc.Controller继承,而Web Form从System.Web.UI.Page继承。两者都是类,什么使得第一个纯粹的和第二个不是? – 2012-01-08 00:08:42

回答

5

ASP.NET MVC和ASP .NET Web窗体共享相同的路由基础设施中,这两个框架最终需要拿出一个IHttpHandler来处理HTTP请求:

IHttpHandler接口一直以来, 开始ASP.NET的一部分,一个Web窗体(一个System.Web.UI.Page)是一个IHttpHandler。

(从问题链接MSDN文章)

在ASP.NET MVC的System.Web.Mvc.MvcHandler类用于,which then delegates to a controller该请求的进一步处理。在ASP.NET Web窗体中,通常使用表示.aspx文件的System.Web.UI.Page类,但也可以使用与.ashx文件关联的IHttpHandler

因此,您可以路由到.ashx处理程序,作为.aspx Web窗体页面的替代方法。两者都实现IHttpHandler(如MvcHandler),但与前者完全相同。这就和你处理(路由)请求的'纯粹类'一样。由于处理程序部分只是一个接口,所以您可以自由地继承自己的类。

<%@ WebHandler Language="C#" Class="LightweightHandler" %> 

using System.Web; 

public class LightweightHandler : YourBaseClass, IHttpHandler 
{ 
    public void ProcessRequest(HttpContext context) 
    { 
    context.Response.ContentType = "text/plain"; 
    context.Response.Write("Hello world!"); 
    } 

    public bool IsReusable { get { return false; } } 
} 

注意的IRouteHandler只需要返回的IHttpHandler一个实例:

public IHttpHandler GetHttpHandler(RequestContext requestContext); 

您可能需要通过一些跳铁圈使用实例化处理程序的BuildManager如果使用.ashx的文件*。如果没有,你可以只新你的类的实例并返回它:

public IHttpHandler GetHttpHandler(RequestContext requestContext) 
{ 
    // In case of an .ashx file, otherwise just new up an instance of a class here 
    IHttpHandler handler = 
    BuildManager.CreateInstanceFromVirtualPath(path, typeof(IHttpHandler)) as IHttpHandler; 

    // Cast to your base class in order to make it work for you 
    YourBaseClass instance = handler as YourBaseClass; 
    instance.Setting = 42; 
    instance.DoWork(); 

    // But return it as an IHttpHandler still, as it needs to do ProcessRequest 
    return handler; 
} 

看到这个问题的答案路由纯IHttpHandlers的更深入的分析:Can ASP.NET Routing be used to create “clean” URLs for .ashx (IHttpHander) handlers?

**我'不完全确定BuildManager的例子,有人请纠正我,如果我得到那部分错误*

+0

谢谢,但仍然为什么我会被迫使用.ashx,因为官方文档提到我可以使用任何东西。我想在MVC Controller路由中这样做:例如,产品的控制器是一个纯粹的类,您可以通过http://domain.com/product – user310291 2012-01-07 22:19:08

+0

@ user310291访问:如果您想要路由*类似于MVC *,那么您需要切换到MVC ....你不能在Webforms中做到这一点。 – 2012-01-07 22:26:28

+0

正如我所说,我不能切换到ASP.NET MVC,从.NET 3.5开始支持路由,为什么我不能这样做? – user310291 2012-01-07 23:08:45

2

如果您不能切换到ASP.NET MVC和路由.ashx处理程序不符合您的要求,您可能想看看南希,'lightweight web framework'。

下面是引进一个例子(见前面的段落链接):

public class Module : NancyModule 
{ 
    public Module() : base("/foo") 
    { 
    Get["/"] = parameters => { 
     return "This is the site route"; 
    }; 

    Delete["/product/{id}"] = parameters => { 
     return string.Concat("You requested that the following product should be deleted: ", parameters.id); 
    }; 
    } 
} 

这个类将处理请求/ foo和/富/产品/ 42。您也可以使用此框架的视图来呈现更复杂的(HTML)响应。

+0

不能在这里使用任何框架,只有.NET本身,所以我的问题:) – user310291 2012-01-08 12:01:43

2

如果您可以从3.5更新到4.0,WebForms支持更好的路由。在Global.asax中,你只需要做这样的事情:

void Application_Start(object sender, EventArgs e) 
{ 
    RouteTable.Routes.MapPageRoute("default", string.Empty, "~/default.aspx");  
} 

我真的不明白“纯A类”的一部分,但希望如果更新到4.0是一个选项,这可以让你去。