简体   繁体   English

在 MVC 控制器中使用查询字符串变量

[英]Use querystring variables in MVC controller

I am new to C#.net MVC and am trying to add FullCalendar to an MVC application.我是 C#.net MVC 的FullCalendar ,正在尝试将FullCalendar添加到 MVC 应用程序。

The FullCalendar script automatically adds ?start={}&end={} to the URL...which is fine, but I have no idea how to use querystring variables in a controller. FullCalendar脚本会自动将?start={}&end={}到 URL...这很好,但我不知道如何在控制器中使用querystring变量。

The methods I used in Webforms applications don't work.我在 Webforms 应用程序中使用的方法不起作用。 What do I need to add to the controller to access the querystring variables?我需要向控制器添加什么才能访问querystring变量?

public ActionResult SomeAction(string start, string end)

该框架将查询字符串参数映射到方法参数。

I figured it out...finally found another article on it.我想通了……终于找到了另一篇关于它的文章

string start = Request.QueryString["start"];

string end = Request.QueryString["end"];

Davids, I had the exact same problem as you.大卫,我和你遇到了完全相同的问题。 MVC is not intuitive and it seems when they designed it the kiddos didn't understand the purpose or importance of an intuitive querystring system for MVC. MVC 并不直观,而且当他们设计它时,孩子们似乎不了解 MVC 的直观查询字符串系统的目的或重要性。

Querystrings are not set in the routes at all (RouteConfig).根本没有在路由中设置查询字符串 (RouteConfig)。 They are add-on "extra" parameters to Actions in the Controller.它们是控制器中操作的附加“额外”参数。 This is very confusing as the Action parameters are designed to process BOTH paths AND Querystrings.这非常令人困惑,因为 Action 参数旨在处理两个路径和查询字符串。 If you added parameters and they did not work, add a second one for the querystring as so:如果您添加了参数但它们不起作用,请为查询字符串添加第二个参数,如下所示:

This would be your action in your Controller class that catches the ID (which is actually just a path set in your RouteConfig file as a typical default path in MVC):这将是您在 Controller 类中捕获 ID 的操作(它实际上只是在 RouteConfig 文件中设置的路径,作为 MVC 中的典型默认路径):

public ActionResult Hello(int id)

But to catch querystrings an additional parameter in your Controller needs to be the added (which is NOT set in your RouteConfig file, by the way):但是要捕获查询字符串,需要在控制器中添加一个附加参数(顺便说一下,该参数未在您的 RouteConfig 文件中设置):

public ActionResult Hello(int id, string start, string end)

This now listens for "/Hello?start=&end=" or "/Hello/?start=&end=" or "/Hello/45?start=&end=" assuming the "id" is set to optional in the RouteConfig.cs file.现在监听 "/Hello?start=&end=" or "/Hello/?start=&end=" or "/Hello/45?start=&end=" 假设 "id" 在 RouteConfig.cs 中设置为可选文件。

If you wanted to create a "custom route" in the RouteConfig file that has no "id" path, you could leave off the "id" or other parameter after the action in that file.如果您想在 RouteConfig 文件中创建一个没有“id”路径的“自定义路由”,您可以在该文件中的操作之后省略“id”或其他参数。 In that case your parameters in your Action method in the controller would process just querystrings.在这种情况下,控制器中 Action 方法中的参数将只处理查询字符串。

I found this extremely confusing myself so you are not alone!我发现这让我自己非常困惑,所以你并不孤单! They should have designed a simple way to add querystring routes for both specific named strings, any querystring name, and any number of querystrings in the RouteConfig file configuration design.他们应该设计了一种简单的方法来为 RouteConfig 文件配置设计中的特定命名字符串、任何查询字符串名称和任意数量的查询字符串添加查询字符串路由。 By not doing that it leaves the whole use of querystrings in MVC web applications as questionable, which is pretty bizarre since querystrings have been a stable part of the World Wide Web since the mid-1990's.如果不这样做,它会使 MVC Web 应用程序中查询字符串的整个使用变得可疑,这很奇怪,因为自 1990 年代中期以来,查询字符串一直是万维网的稳定部分。 :( :(

Here is what I came up with.这是我想出的。 I was having major problems with this and believe I am in MVC 6 now but this may be helpful to someone even myself in the future..我在这方面遇到了重大问题,并且相信我现在在 MVC 6 中,但这可能对将来甚至我自己都有帮助。

//The issue was that Reqest.Form Request.Querystring and Request not working in MVC the solution is to use Context.Request.Form and also making sure the form has been submitted otherwise null reference or context issue bug will show up. //问题是 Reqest.Form Request.Querystring 和 Request 在 MVC 中不起作用,解决方案是使用 Context.Request.Form 并确保表单已提交,否则会出现空引用或上下文问题错误。

     if(Context.Request.ContentLength != null)
     {
         String StartDate = Context.Request.Form["StartMonth"].ToString();
         String EndMonth = Context.Request.Form["EndMonth"].ToString();
        // Vendor

     }

My problem was overwriting my query string parameters with default values:我的问题是用默认值覆盖我的查询字符串参数:

routes.MapRoute(
    "apiRoute", 
    "api/{action}/{key}", 
    new { controller = "Api", action = "Prices", key = ""}
);

No matter what I plugged into query string or how only key="" results.无论我插入查询字符串还是只有key=""结果。

Then got rid of default overwrites using UrlParameter.Optional:然后使用 UrlParameter.Optional 摆脱默认覆盖:

routes.MapRoute(
    "apiRoute", 
    "api/{action}/{key}", 
    new { controller = "Api", action = "Prices", key = UrlParameter.Optional }
);

now现在

prices/{key} 

or或者

prices?key={key} 

both work fine.两者都工作正常。

I had this problem while inheriting from ApiController instead of the regular Controller class.我在继承 ApiController 而不是常规的 Controller 类时遇到了这个问题。 I solved it by using var container = Request.GetQueryNameValuePairs().ToLookup(x => x.Key, x => x.Value);我通过使用var container = Request.GetQueryNameValuePairs().ToLookup(x => x.Key, x => x.Value);解决了它var container = Request.GetQueryNameValuePairs().ToLookup(x => x.Key, x => x.Value);

I followed this thread How to get Request Querystring values?我关注了这个线程如何获取请求查询字符串值?

EDIT: After trying to filter through the container I was getting odd error messages.编辑:在尝试过滤容器后,我收到了奇怪的错误消息。 After going to my project properties and I unchecked the Optimize Code checkbox which changed so that all of a sudden the parameters in my controller where filled up from the url as I wanted.转到我的项目属性后,我取消选中 Optimize Code 复选框,该复选框发生了变化,因此突然间控制器中的参数根据我的需要从 url 中填充。

Hopefully this will help someone with the same problem..希望这会帮助有同样问题的人..

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

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