在url中使用一组固定参数的操作以及来自post或querystring的一组可选参数

时间:2009-09-22 13:48:00

标签: c# .net asp.net-mvc query-string model-binding

在我的路线收藏中,我有

routes.MapRoute(   “重新计算”,   “{} siteLanguage {/Formula.mvc/relcalculate/ identifierString} / {formulaId} / {wratio} / {customRecalcString}”,   new {controller =“Formula”,action =“Recalculate”,wratio =“”,customRecalcString =“”}  );

它映射的动作是

public ActionResult Recalculate(string identifierString, int formulaId, string wratio, string customRecalcString, string targetQuantity, int? targetUnit)

正如您所看到的,路线描述中没有severa参数。 这些是可选参数 当我进入POST时,我在表单中放置的值被正确映射到这些参数 但是,当我使用GET输入时,变量将添加到查询字符串中:

/en/Formula.mvc/relcalculate/493133---GREEN%20TILE%20TEST%20PLAQUE---SPECTROSYSTEM/2972591?targetQuantity=1&targetUnit=1

但是当到达动作时,参数和查询字符串是空的......

有什么想法吗?

修改
似乎查询字符串中途消失了 改变OP以反映这一点 的 EDIT2
经过进一步调查后,似乎再次归结为asp.net-mvc的编码算法似乎失败了(或者是我?) 如果我在浏览器地址栏中填写了URL,它就能完美运行。 只有当字符串用作action =“”属性值时,查询字符串才会消失。 如果我没有误解'&'应该编码查询字符串。

为什么Url.RouteUrl()不这样做?
为什么Url.Encode()给了我各种公牛? 如何在不编码值的情况下仅编码查询字符串部分?
EDIT3
我用Html.Encode()包围了这个动作,我应该从开始的课程开始。现在页面再次验证..但无济于事。
Edit4
发现它!
基本上这是HTML失败的

<form action="/en/Formula.mvc/relcalculate/493133---GREEN%20TILE%20TEST%20PLAQUE---SPECTROSYSTEM/2972591?targetQuantity=50&amp;targetUnit=0" method="get">
    <div>
        <input type="submit" class="noPrint" value="Remove RFU" />
    </div>
</form>

一旦我将表单的方法从get更改为post,一切都运行正常 花了2个小时! 的更新
回应评论:
关于不披露的偏执狂,我会遗漏一些东西,但这是页面的要点:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="ReadyForUse.aspx.cs" Inherits="CommonProject.Web.Shared.Views.ProductMix.ReadyForUse" %>
<asp:Content ContentPlaceHolderID="headerContentPlaceHolder" runat="server">
    <link href="<%=PathHelper.CssUrl("productMix.css")%>" rel="Stylesheet" type="text/css"/>    
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="mainContentPlaceHolder" runat="server">
    <%string action = Url.RouteUrl("Recalculate",
          new { 
              siteLanguage = ViewData.Model.SiteLanguage.Iso6391[0],
              identifierString = ColorService.GetIdentifierString(ViewData.Model.ActiveColor),
              formulaId = ViewData.Model.ActiveFormula.FormulaId,
              targetQuantity = FormulaService.ParseQuantityFromVolumeString(ViewData.Model.TargetVolume),
              targetUnit = FormulaService.ParseUnitFromVolumeString(ViewData.Model.TargetVolume).Id,
              wratio = ViewData.Model.Wratio.ToString(),
              customRecalcString = ProductMixService.GetCustomRecalcValuesString(ViewData.Model.GaugesStructure)
          });
      string removeAction = Url.RouteUrl("Recalculate",
          new { 
              siteLanguage = ViewData.Model.SiteLanguage.Iso6391[0],
              identifierString = ColorService.GetIdentifierString(ViewData.Model.ActiveColor),
              formulaId = ViewData.Model.ActiveFormula.FormulaId,
              targetQuantity = FormulaService.ParseQuantityFromVolumeString(ViewData.Model.TargetVolume).ToString(),
              targetUnit = FormulaService.ParseUnitFromVolumeString(ViewData.Model.TargetVolume).Id,
          });
    %> 
    <snip/>
    <table width="100%">
        <tr>
            <td width="50%">
                <div>   
                    <a href="<%=Html.Encode(action)%>" alt="test">thisWorks</a>
                    <form action="<%=Html.Encode(action)%>" method="get">
                        <div>
                            <input type="submit" value="thisDoesnt" />
                        </div>
                    </form>
                </div>
            </td>
            <td width="50%">
                <div>                
                    <a href="<%=Html.Encode(removeAction)%>" alt="test">thisWorks</a>
                    <form action="<%=Html.Encode(removeAction)%>" method="get">
                        <div>                                        
                            <input type="submit" value="thisDoesnt" />
                        </div>
                    </form>
                </div>
            </td>
        </tr>
    </table>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="javaScriptPlaceHolder" runat="server">
    <script src="/Content/scripts/effects.core.min.js" type="text/javascript"></script>
    <script src="<%=PathHelper.JsUrl("productMix.js")%>" type="text/javascript"></script>
</asp:Content>

我倾向于在页面开头定义动作网址,这样我就可以轻松找到它并且不会乱丢我的页面。此外,我知道这张桌子是亵渎神灵,但是我昨天浪费的时间将暂时不得不这样做。我将不得不在以后的周期中支付程序员的税。

1 个答案:

答案 0 :(得分:0)

我刚刚创建了sample application =&gt;

控制器:

 public class FooController : Controller
    {
        public ActionResult Bar
            (string fizz1, int fizz2, string fizz3, string fizz4, 
             string fizz5, int? fizz6)
        {
            var sb = new StringBuilder();

            sb.AppendLine(string.Format("{0} | {1} | {2} | {3} | {4} | {5}", 
                fizz1, fizz2, fizz3, fizz4, fizz5, fizz6.HasValue ? fizz6.Value.ToString():""));

            return Content(sb.ToString());
        }

    }

global.asax:

 public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            //routes.MapRoute(
            //    "Default",                                              // Route name
            //    "{controller}/{action}/{id}",                           // URL with parameters
            //    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            //);

            routes.MapRoute(
                    "FooBar",
                    "{siteLanguage}/bar/{fizz1}/{fizz2}/{fizz3}/{fizz4}",
                    new { controller = "Foo", action = "Bar", fizz3 = "", fizz4 = "" }
            );

        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
        }
    }

结果:

  

http://localhost:4038/en/bar/fizz1/2?fizz5=val&fizz6=1

返回

  

fizz1 | 2 | | | val | 1

问题似乎在其他地方。是否有任何过滤器/模型粘合剂做某事?