简体   繁体   English

ASP.NET MVC删除控制器QueryString操作

[英]ASP.NET MVC Removed Controller QueryString Action

When I use the default route: 当我使用默认路由时:

routes.MapRoute(
"Default", // 
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional } );

The app finds the method below: 该应用程序找到以下方法:

public ActionResult News(DateTime? FromDate, DateTime? ToDate)
{
    IList<Model.MonthGroupNewsEntry> monthGroupNewsEntries = new services.NewsEntryService(new Data.SqlNewsEntryRepository()).GetMonthGroupNewsEntries();
    IList<Model.NewsEntry> newsEntries = new Services.NewsEntryService(new Data.SqlNewsEntryRepository()).GetNewsEntriesWithinDateRange(FromDate, 
    return View(new Models.NewsViewModel(monthGroupNewsEntries, newsEntries));
}

From: 从:

<%= Html.ActionLink(b.MonthName + " " + b.Year.ToString() +  " (" + b.EntryCount.ToString() + ")", "News", new { FromDate = b.FromDate, ToDate = b.ToDate}) %>

But when I attempt to remove the controller from the url using below: 但是,当我尝试使用以下方法从网址中删除控制器时:

routes.MapRoute(
"Default",
"{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional } );

I get a 404 resource not found error. 我收到404资源未找到错误。

Note : The other methods in the class work fine but these either have no parameters or "id", it's only the method that's called with the two DateTime? 注意 :类中的其他方法可以正常工作,但是这些方法都没有参数或“ id”,这只是两个DateTime调用的方法? parameters that fails when I remove the controller from the URL. 从URL删除控制器时失败的参数。

Thanks for the updated answer, however - the only change I make is to remove the "{controller}/" text from the "routes.MapRoute" in the above - there is no chance of any spelling mistake etc., this is also using MVC 3. 感谢您提供更新的答案,但是-我所做的唯一更改是从上述“ routes.MapRoute”中删除了“ {controller} /”文本-没有任何拼写错误的机会,等等。 MVC 3。

Further : using Charx's suggestion: 进一步 :使用Charx的建议:

routes.MapRoute(
"SpecificAction",
"{action}/{FromDate}/{ToDate}",
new { controller = "ControllerName", id = UrlParameter.Optional } );

I no longer get a 404 error, but it gave a "400 - bad request" due to the '/'s in the date, so I reformatted the date as dd-MM-YYYY: 我不再收到404错误,但是由于日期中的'/'导致了“ 400-错误的请求”,因此我将日期重新格式化为dd-MM-YYYY:

<%= Html.ActionLink(b.MonthName + " " + b.Year.ToString() +  " (" + b.EntryCount.ToString() + ")", "News", new { FromDate = b.FromDate.ToString("dd-MM-yyyy"), ToDate = b.ToDate.ToString("dd-MM-yyyy")}) %>

It no longer 404's, the FromDate is recognised fine but the ToDate comes through as null. 它不再是404,可以很好地识别FromDate,但是ToDate可以作为null。

Final : I changed the type of the two dates to strings and cast them to datetime in the method - now both parameters come through fine - I guess it was perhaps looking for the time element of the first parameter and therefore ignored the second. 最终 :我将两个日期的类型更改为字符串,并在方法中将它们强制转换为日期时间-现在两个参数都可以正常运行-我想这可能是在寻找第一个参数的时间元素,因此忽略了第二个。

Only outstanding thing now is that the methods requiring an "id" parameter now seems to use it as a querystring (NewsEnrty?ID=5) rather than "NewsEntry/5" if anyone can answer this then thanks - otherwise question answered - thanks again Chanx. 现在唯一值得一提的是,现在需要“ id”参数的方法似乎将其用作查询字符串(NewsEnrty?ID = 5),而不是“ NewsEntry / 5”,如果有人可以回答的话,那么谢谢-否则问题已解决-再次感谢昌克斯

Ok, when you edit your question I get the initial answer you want to have. 好的,当您编辑问题时,我会得到您想要的初始答案。 I also create simple solution to test your problem and it works well also for News controller action. 我还创建了简单的解决方案来测试您的问题,并且对于新闻控制器操作也很有效。 There are no way how dates could affect routing. 日期无法影响路由。 But you need to check firstly - maybe you made some mistakes. 但是您需要首先检查-也许您犯了一些错误。 Check for routing once again, chech controller name, retype it, chech that proper action placed n prpper controller. 再次检查路由,检查控制器名称,重新输入,检查是否将正确的动作放置在了prpper控制器上。 If you still will have the problem create a small project for your problem and putit on dropbox or somewhere else where I can get it and test, because for now I can't reproduce your issue. 如果您仍然遇到问题,请为您的问题创建一个小项目,然后将其放入Dropbox或其他我可以进行测试的地方,因为到目前为止,我无法重现您的问题。 Also, I;m working with MVC 3. 另外,我正在使用MVC 3。

Edited. 编辑。 If you want to still be able to identify the action to call in your URL, then you must specify your Specific Action route and be sure to identify the correct Controller the actions are on: 如果您仍然希望能够识别要在URL中调用的操作,则必须指定“特定操作”路由,并确保识别出操作所针对的正确Controller:

routes.MapRoute(
"SpecificAction",
"{action}/{FromDate}/{ToDate}",
new { controller = "ControllerName", id = UrlParameter.Optional } );

routes.MapRoute(
"Default", // 
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional } );

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

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