简体   繁体   English

返回集合的我的Asp.net mvc操作生成包含system.linq.Enumerable的URL,如何删除它们

[英]My Asp.net mvc actions that return collection generate urls containing system.linq.Enumerable, how to remove them

I have an mvc application that basically generates views which are bascially collections but collections of a particular type..so ie 我有一个mvc应用程序,该应用程序基本上生成视图,这些视图基本上是集合,但是具有特定类型的集合。

IEnumerable<IType>

Now my urls contain stuff like this 现在我的网址包含这样的内容

www.site/home/section/param?System.Linq.Enumerable%..... www.site/home/section/param?System.Linq.Enumerable%.....

I want to remove anything after section. 我想在部分后删除任何内容。

I have tried the routeMap but have been unable to ommit the system.linq etc. Any ideas or help please 我已经尝试过routeMap,但无法省略system.linq等。任何想法或帮助,请

Here is my action method 这是我的动作方法

public ActionResult Whatever(IEnumerable<IType> whatever)
        {
            return View(whatever);
        }

You can't pass around collections like that in the URL, you shouldn't add them to the RouteValueDictionary when you redirect. 您不能在URL中传递类似的集合,重定向时不应将它们添加到RouteValueDictionary Just send some basic information that can help you get what you need in the action you are redirecting to. 只需发送一些基本信息即可帮助您在重定向到的操作中获得所需的信息。

Edit: Based on your code, we can see that your action method takes an IEnumerable , so wherever you are calling this action, you're passing it one. 编辑:根据您的代码,我们可以看到您的操作方法采用IEnumerable ,因此无论您在何处调用此操作,都将其传递给它。 You can't do that , you'll have to generate your list in the action method. 不能这样做,必须在action方法中生成列表。 Try something like: 尝试类似:

public ActionResult Whatever()
{
    List<IType> whatever = new List<IType>();

    //populate your list here, then we can return it

    return View(whatever);
}

The question above is a very interesting and took me a while and has go back to my previously written mvc app. 上面的问题是一个非常有趣的问题,花了我一段时间,回到了我之前编写的mvc应用程序。 The answer is simple but not obvious. 答案很简单,但并不明显。

And it seems in the current mvc3 even if you pass the object as anonymous type mapping your view to your view model, it always works out the collectiontype and somehow appends that to the url in this case it was appending whole case to the url as 而且在当前的mvc3中,即使您将对象作为匿名类型传递给您的视图到视图模型,它始终可以计算出collectiontype并以某种方式将其附加到url,在这种情况下,它会将整个大小写附加到url上

http://www.website.com/param=system.linq.enumerable.where.select .. http://www.website.com/param=system.linq.enumerable.where.select ..

any way the correct way is to wrap it in a routevaluedictionary 任何正确的方法都是将其包装在routevaluedictionary中

new RouteValueDictionary(new { controller = Constants.HOMECONTROLLER, action = Constants.APPLYAPP })); new RouteValueDictionary(new {controller = Constants.HOMECONTROLLER,action = Constants.APPLYAPP}));

and if you are passing any stuff from one action to another use this 如果您将任何东西从一个动作传递到另一个动作,请使用此

return new RedirectToRouteResult(Constants.DEFAULTROUTE,
                     new RouteValueDictionary(new { controller = Constants.HOMECONTROLLER, action = Constants.APPLYAPP }));

instead of RedirecttoAction because it seems this results in the url above. 而不是RedirecttoAction,因为看起来这会导致上述网址。

Thanks MattyTommo for the help but its irrelevant in my case but it may be helpful for others. 感谢MattyTommo的帮助,但与我的情况无关,但对其他人可能有所帮助。 I cannot mark mattytommo's reply as answer at all because it actually misunderstood my request, perhaps my request wasnt correct but also misguided me. 我根本无法将mattytommo的答复标记为答案,因为它实际上误解了我的请求,也许我的请求不正确,但也误导了我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM