在ASP中使用相同名称的多个参数重定向。净MVC吗?

时间:2022-05-12 16:47:34

I am trying to implement POST-Redirect-GET in MVC using multiple named parameters.

我正在尝试使用多个命名参数在MVC中实现后直接获取。

I can do this with one parameter:

我只需要一个参数:

return RedirectToAction("MyGetView", new { bookId = id });

I could hardcode multiple parameters:

我可以硬编码多个参数:

return RedirectToAction("MyGetView, new {bookId = id1, bookId=id2});

But how do I get from an IEnumerable< int> of Ids, which is of variable length, to a correct query string without constructing it by hand?

但是,如何从IEnumerable(可变长度的id)到正确的查询字符串,而不用手工构造它呢?

My current code looks like this:

我当前的代码是这样的:

var querystring= string.Join("", BookIds.Select(x => string.Format("bookId={0}&", x)));
querystring= querystring.Trim('&');

return Redirect("MyGetView?" + querystring);

It works fine, but it seems like there should be a better way.

它运行良好,但似乎应该有更好的方法。

(I want the parameters to be visible in the URL, so that users can bookmark the page. I believe this means I cannot use TempData.)

(我希望参数在URL中是可见的,以便用户可以对页面进行书签。我相信这意味着我不能使用TempData。

1 个解决方案

#1


1  

First, it's not wrong to use query parameters where the ASP.NET MVC Routes fail to work, and they do with arrays (basically). How is your "clean" URL route supposed to look like?

首先,在ASP中使用查询参数是正确的。NET MVC路由失败,它们使用数组(基本上)。你的“清洁”URL路径应该是什么样的?

Example: /Books/View/6 (Works nice for one Book, 6 is the int ID)
But what do you want to have for multiple Books? /Books/View/6,5,134 maybe?

例如:/Books/View/6(适合一本书,6是int ID)但是对于多本书你想要什么呢?/书/视图/ 6,5134也许?

In this case you could just use your own convention of formatting the Url as a list of IDs.

在这种情况下,您可以使用自己的约定,将Url格式化为id列表。

Your redirect would look like: return RedirectToAction("View", "Book", new { Id = allIds.Join(",") });

您的重定向看起来是:返回RedirectToAction(“View”、“Book”、new {Id = allidas . join(“”)});

And your View action can support this:

你的观点行动可以支持这一点:

public ActionResult View(string id)
{
    if (id == null)
        throw new Exception... // Not expected

    var ids = id.Split(new char[]{ ',' });

    // Further processing...
}

If your're not satisfied with this approach (you might have issues when number of items is getting bigger) you can see what others tried, but it's basically what you already do, or I'm not sure if the other solutions are worth the effort.

如果您对这种方法不满意(当项目的数量越来越大时,您可能会遇到问题),您可以看到其他人尝试了什么,但是这基本上是您已经做的,或者我不确定其他的解决方案是否值得。

#1


1  

First, it's not wrong to use query parameters where the ASP.NET MVC Routes fail to work, and they do with arrays (basically). How is your "clean" URL route supposed to look like?

首先,在ASP中使用查询参数是正确的。NET MVC路由失败,它们使用数组(基本上)。你的“清洁”URL路径应该是什么样的?

Example: /Books/View/6 (Works nice for one Book, 6 is the int ID)
But what do you want to have for multiple Books? /Books/View/6,5,134 maybe?

例如:/Books/View/6(适合一本书,6是int ID)但是对于多本书你想要什么呢?/书/视图/ 6,5134也许?

In this case you could just use your own convention of formatting the Url as a list of IDs.

在这种情况下,您可以使用自己的约定,将Url格式化为id列表。

Your redirect would look like: return RedirectToAction("View", "Book", new { Id = allIds.Join(",") });

您的重定向看起来是:返回RedirectToAction(“View”、“Book”、new {Id = allidas . join(“”)});

And your View action can support this:

你的观点行动可以支持这一点:

public ActionResult View(string id)
{
    if (id == null)
        throw new Exception... // Not expected

    var ids = id.Split(new char[]{ ',' });

    // Further processing...
}

If your're not satisfied with this approach (you might have issues when number of items is getting bigger) you can see what others tried, but it's basically what you already do, or I'm not sure if the other solutions are worth the effort.

如果您对这种方法不满意(当项目的数量越来越大时,您可能会遇到问题),您可以看到其他人尝试了什么,但是这基本上是您已经做的,或者我不确定其他的解决方案是否值得。