在asp.net mvc中路由webform

时间:2010-08-06 20:07:15

标签: asp.net .net asp.net-mvc asp.net-mvc-2 asp.net-mvc-routing

我有一个ASP.NET MVC应用程序,我有一个我在MVC中构建的WebForm页面,因为有关如何做我需要做的事情的教程,但它都是WebForm风格。我试图弄清楚如何在MVC格式中做同样的事情,但无法弄明白。所以我需要弄清楚如何在我的MVC应用程序中使用此页面。但是当我尝试转到页面时,它会给出错误“除非PageView派生自ViewPage,否则页面无法从ViewMasterPage派生。”所以我不得不制作一个新的标准MasterPage。

情况就是这样。我有一个位于MVC ViewMasterPage中的搜索栏,它位于从中派生的每个页面上。一旦用户在搜索栏中提交信息,它就会调用WebForm Search.aspx页面并在Search.aspx页面上显示结果。我希望URL类似于“http:/// search //。Search.aspx页面位于项目的根目录中。如何获得我想要的结果?谢谢!

2 个答案:

答案 0 :(得分:3)

您可能希望在适当的MVC中重新实现该Web表单。如果你知道自己在做什么,那么两个人可以在同一个网络应用程序中一起玩得很好,但是如果你想在MVC中实现整个事情,那么只需坚持偶尔页面的Web表单教程即可整个事情更难以支持。 (正如您已经从必须制作第二个母版页中学到的那样。)

研究教程,但要明确了解它们正在做什么,而不仅仅是复制/粘贴代码。结束时的实际实现听起来应该保留在MVC中。

答案 1 :(得分:1)

ASP.NET WebForms可以绝对处理路由。我最近构建了一个非常大的站点,完全是WebForms并完全路由。没有公开表明它是Viewstate之外的aspx站点,而另一个是垃圾站。

<system.webServer>
  <rewrite>
    <rules>
      <!-- If a user requests the search.aspx page, redirect them to /search -->
      <rule name="Search-Redirect" stopProcessing="true">
        <match url="^search\.aspx$" />
        <conditions>
          <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
        </conditions>
        <action type="Redirect" url="search" appendQueryString="true" />
      </rule>
      <!-- If a user requests the search page with a query appended to the URL, send them to the search.aspx page and put the training URL into the q query string -->
      <rule name="Search-Query-Rewrite" stopProcessing="true">
        <match url="^search/([_0-9a-z-]+)" />
        <action type="Rewrite" url="search.aspx?q={R:1}" appendQueryString="false" />
      </rule>
      <!-- If a user requests the search page without a query appended, just send them to the search.aspx page -->
      <rule name="Search-Rewrite" stopProcessing="true">
        <match url="^search$" />
        <action type="Rewrite" url="search.aspx" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

根据您的网址结构,您可能需要稍微调整一下,您希望搜索条件如何进入页面,但它会起作用。