ASP.NET 网站上的路由(不是 WebForm 应用程序,也不是 MVC)

发布于 2024-12-07 21:49:22 字数 3200 浏览 0 评论 0原文

我可以使用 web.config 或 Global.asax 在 asp.net 网站(文件夹结构)中进行路由吗?

基本上我想要

http://www.something.com/agents/10/TheAgentsName

转到

http://www.something.com/portfolio.aspx?caid=10

代理名称仅用于 URL 中的“装饰”。

我找到了几个示例,但它们都适用于 MVC 或 WebForms 应用程序。

当尝试实现 Global.asax 时,它一直说命名空间中不存在 System.Web.Routing。这就是我的 Global.asax 现在的样子(它无法运行)!

<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web.Routing" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        RegisterRoutes(System.Web.Routing.RouteTable.Routes);

    }

    void RegisterRoutes(System.Web.Routing.RouteCollection routes)
    {
        routes.MapPageRoute("AgentPortfolioByName", "agents/{id}/{name}", "portfolio.aspx");
    }

    void Application_End(object sender, EventArgs e) 
    {
    }

    void Application_Error(object sender, EventArgs e) 
    { 
    }

    void Session_Start(object sender, EventArgs e) 
    {
    }

    void Session_End(object sender, EventArgs e) 
    {
    }

</script>

Global.asax Application_Start 执行得很好(我试图设置一个断点)。但它只是不明白 System.Web.Routing 是什么。 web.config 中我有这些条目:

<compilation debug="true">
            <assemblies>
                <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/></assemblies>
        </compilation>

............

<httpModules>
            <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add name="RoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        </httpModules>

<system.webServer>
        <validation validateIntegratedModeConfiguration="false"/>
        <modules>
            <remove name="ScriptModule"/>
            <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        </modules>

Can I use web.config or Global.asax to do routing in an asp.net website (folder structure)?

Basically I would like

http://www.something.com/agents/10/TheAgentsName

to go to

http://www.something.com/portfolio.aspx?caid=10

The agents name is just for "decoration" in the URL.

I have found several examples but they all apply to MVC or WebForms apps.

When trying to implement Global.asax, it keeps saying that System.Web.Routing does not exist in the namespace. This is what my Global.asax looks like now (it's unable to run)!

<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web.Routing" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        RegisterRoutes(System.Web.Routing.RouteTable.Routes);

    }

    void RegisterRoutes(System.Web.Routing.RouteCollection routes)
    {
        routes.MapPageRoute("AgentPortfolioByName", "agents/{id}/{name}", "portfolio.aspx");
    }

    void Application_End(object sender, EventArgs e) 
    {
    }

    void Application_Error(object sender, EventArgs e) 
    { 
    }

    void Session_Start(object sender, EventArgs e) 
    {
    }

    void Session_End(object sender, EventArgs e) 
    {
    }

</script>

The Global.asax Application_Start is executed fine (I tried to put a break-point). But it just does not understand what System.Web.Routing is. In web.config I have these entries:

<compilation debug="true">
            <assemblies>
                <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/></assemblies>
        </compilation>

......

<httpModules>
            <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add name="RoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        </httpModules>

........

<system.webServer>
        <validation validateIntegratedModeConfiguration="false"/>
        <modules>
            <remove name="ScriptModule"/>
            <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        </modules>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场信仰旅途 2024-12-14 21:49:22

您可以使用 Global.asax 来实现路由。我认为您在应用程序中缺少 System.Web.Routing.dll 引用。请确保您引用的是这个。另请确保在 Global.asax 中导入 System.Web.Routing 命名空间。如果以下链接有助于理解在网站应用程序中处理 Global.asax 文件,请访问以下链接。

http://beyondrelational.com/blogs/niladribiswas/archive/2011/06/09/adding-the-global-asax-cs-file-in-asp-net-web-site.aspx< /a>

请随时联系以进行进一步讨论。

谢谢,

You can use Global.asax for implement routing. I think you are somehow missing the System.Web.Routing.dll reference in your application. Please make sure you are referencing this one. Also please make sure in Global.asax you are importing System.Web.Routing namespace. Please go through below link if it will be helpful in understanding the handling Global.asax file in web site application.

http://beyondrelational.com/blogs/niladribiswas/archive/2011/06/09/adding-the-global-asax-cs-file-in-asp-net-web-site.aspx

Please fell free to contact for further discussion.

Thanks,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文