作为MVC 4的新手(或者实际上是ASP.NET的任何MVC),我无法帮助,但感觉URL帮助程序比我看到的更多。
基本上我已经阅读了使用URL中的查询字符串在控制器方法中填充属性的教程。 我不会欺骗查询字符串,而更喜欢像样式一样的分段“文件夹”。
没有更多的adu,这是示例URL:
http://something.com/DataTypes/Search/searchString
这种方法实际上非常安全,因为只有单一的措辞搜索
我试过DataTypes控制器
[HttpGet] public ActionResult Search(String q) { ViewBag.ProvidedQuery = q; return View(); }还有一些其他的小变化,现在我只是试图让字符串显示在视图中,但我似乎没有得到任何东西。
无论如何将url中的第3个字符串注入属性? 如果没有,我应该使用哪个URL帮助程序类来获取URL中的字符串数据? 即使我必须手动解析整个URL,所以就这样,我只需要将URL中的第3个元素作为字符串获取
非常n00b问题我肯定,但要么我没有在某处找到这个简单的指南,或者我没有正确搜索谷歌...
Being kind of a newb to MVC 4 (or really any of the MVC's for ASP.NET) I cant help but feel theres more to the URL helper than what I'm seeing.
Basically I've read the tutorials on populating the attributes in a controllers methods using a query string in the URL. I dont liek query strings though and prefer a sectioned "folder" like style.
Without much further adu, this is the sample URL:
http://something.com/DataTypes/Search/searchString
this approach is actually pretty safe as there will only ever be single worded searches
I have tried in the DataTypes controller
[HttpGet] public ActionResult Search(String q) { ViewBag.ProvidedQuery = q; return View(); }and a few other small variations, right now im just trying to get the string to show up in the view but I dont seem to be getting anything there.
Is there anyway to inject the 3rd string in the url into an attribute? If not, which URL helper class am I supposed to use to acquire the string data in the URL? Even if I have to parse the whole URL manually so be it, i just need to acquire that 3rd element in the URL as a string
Extremely n00b question im sure, but either im not finding this simple guide somewhere, or im not searching google correctly...
最满意答案
您缺少的是默认路由参数名称是“id”。 所以你想这样做:
[HttpGet] public ActionResult Search(String id) { ViewBag.ProvidedQuery = id; return View(); }如果您不想使用变量名称id,则可以将Route修改为如下所示:
routes.MapRoute( name: "Search", url: "DataTypes/Search/{searchString}", defaults: new { controller = "DataTypes", action = "Search", searchString = UrlParameter.Optional });如果您不希望该字符串是可选的,则可以从defaults对象中删除最后一个字段。
What you're missing is that the default route parameter name is "id". So you want to do this:
[HttpGet] public ActionResult Search(String id) { ViewBag.ProvidedQuery = id; return View(); }If you don't want to use the variable name id, then you can modify your Route to look like this:
routes.MapRoute( name: "Search", url: "DataTypes/Search/{searchString}", defaults: new { controller = "DataTypes", action = "Search", searchString = UrlParameter.Optional });If you don't want the string to be optional, then you can remove the last field from the defaults object.
更多推荐
发布评论