·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> ASP.NET MVC 混搭 ASP.NET WebForms 所导致的 Html.ActionLink/BeginForm 问题

ASP.NET MVC 混搭 ASP.NET WebForms 所导致的 Html.ActionLink/BeginForm 问题

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

asp.net MVC 混搭 ASP.NET WebForms 所导致的 Html.ActionLink/BeginForm 问题

首先,需要了解下这篇博文:《ASP.NET WebForms MapPageRoute 路由配置》

之前,在 ASP.NET MVC 中混搭 ASP.NET WebForms,使用 MapPageRoute 没有出现什么问题,但是今天在测试一个表单提交的时候,却莫名其妙的出现一个诡异的问题,我先大致描述下,就是 MVC View 中使用表单,Html.BeginForm 方法提交到指定控制器的 Action 中进行处理,大致代码为:

@using (Html.BeginForm("PRofileSubmit", "Set", FormMethod.Post, new { id = "myform" })){    @Html.AntiForgeryToken()    <table class="info_block" cellpadding="0" cellspacing="0">        <tr>            <td class="tr_title">                姓名:            </td>            <td class="tr_content">                @Html.TextBoxFor(n => n.RealName, new { id = "txt_name", @class = "tb_m" })            </td>        </tr>    </table>    <div class="save_block">        <input type="submit" class="btn_blue" name="btnSubmit" value="保存" onclick="return UpdateProfile();" id="btn_submit">        <div class="text_red" style="height: 3em; line-height: 3em;">            <span id="lit_tip">@Model.Message</span>        </div>    </div>}

这是我们最普遍的写法,把表单数据提交到 Set 控制器的 ProfileSubmit Action 中进行处理,但是在提交的时候,URL 却变成了:

http://home.cnblogs.com/job/myresume?action=ProfileSubmit&controller=Set

是不是很诡异?我明明在 Html.BeginForm 方法中指定了 Controller 和 Action,为什么没有起作用,反而跳转到了一个莫名其妙的 URL,根据 URL,我就查看了下路由配置,发现下面的一段配置:

routes.MapPageRoute("JobMyResume",    "job/myresume",    "~/old/job/MyResume.aspx");

注意,这段路由配置并不是使用的 MapRoute,而是 MapPageRoute,是配置 ASP.NET WebForms 路由的,我把这段配置注释掉,然后就可以了,有点莫名其妙,为什么使用 Html.BeginForm 会映射到这段路由上?最后搜了大量的资料,也没有找到相关说明,我个人觉得是 MapPageRoute 的问题,ASP.NET MVC 混搭 ASP.NET WebForms,但是在 URL 请求处理的时候,并不是很准确,凡事使用 Html Helps 里面的生成 URL 方法,都存在问题,除了 Html.BeginForm,还有我们常用到的 Html.ActionLink,但是从外部客户端进入的请求,就没有任何问题,最后找了一些相关解决方式,也证明了这一点,解决方式很简单,先添加如下代码:

public class IncomingRequestConstraint : IRouteConstraint{    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)    {        return routeDirection == RouteDirection.IncomingRequest;    }}

然后 MapPageRoute 的路由配置改为:

routes.MapPageRoute("JobMyResume",    "job/myresume",    "~/old/job/MyResume.aspx", false, null, new RouteValueDictionary(new    {        controller = new IncomingRequestConstraint()    }));

什么意思呢?RouteDirection.IncomingRequest 的意思,就是路由约束限制在外部,也就是说只请求来自客户端的 URL,内部的 URL 不匹配此路由,像 Html.ActionLink/BeginForm 是 MVC 内部生成的 URL,所以是不被匹配的,RouteDirection 还有另外一个枚举值 UrlGeneration,它的意思就是只请求内部生成的 URL。

上面这种解决方案,只是临时的解决方案,也就是像打补丁一样的处理方式,关于为什么会出现这个问题?MapPageRoute 具体是怎么进行配置的?ASP.NET MVC 混搭 ASP.NET WebForms 请求到底是怎么处理的?这些还不是很清楚,希望大家可以指教。

  • RouteDirection Enumeration
  • Why does the order of route mapping matter when mixing MVC and webforms?
  • Unexpected form url with mixed aspx & controller MVC .Net routes
  • Combining ASP.NET WebForms and ASP.NET MVC