如果 URL 参数很长,控制器操作不会调用

2024-04-09

仅供参考:我的问题不是重复的MVC 3 中的长 url 为 404.20 https://stackoverflow.com/questions/20798392/404-20-for-long-url-in-mvc-3所以请不要混淆。

我有一个 Asp.net MVC 应用程序,其中有一个接受字符串类型参数的操作方法。其 URL 可能会很长,如下所示。

http://localhost:10537/Search/True_IsFixerUpper/True_IsNewConstruction/True_HasHorses/True_HasVirtualTour/True_HasGarage/True_IsShortSale/True_IsWaterFront/True_HasSwimmingPool/True_HasGolfCourse/True_IsWithinGatedCommunity/True_IsMobileManufacturedHome/True_IsForclosure/True_HasFireplace/True_Is55Community/True_IsWaterfrontRiver/True_IsWaterfrontBay/True_IsWaterfrontInteriorCanal/True_IsWaterfrontOcean/True_IsWaterfrontOceanAccess/True_IsWaterfrontIntracoastal/True_IsWaterfrontLake/True_HasViewOcean/True_HasViewGarden/True_HasViewGolfCourse/True_HasViewRiver/True_HasViewCanal/True_HasViewPond/True_HasViewLake/True_HasViewPool/True_HasPhotos/True_IsOpenHouse/True_IsFenced/True_IsNavigable/True_IsAttached/True_IsDetached/True_IsSemiDetached/True_IsOneStory/True_IsTwoStory/True_IsNonMls/True_IsBoatHouse/True_IsBoatSlip/True_IsDockMooring/

上面报错了HTTP 错误 404.20 - 未找到

Most likely causes:
    A default document is not configured for the site.
    The URL contains a typographical error.
    Directory browsing is not enabled.

Things you can try:

    Configure a default document for this site. This is commonly default.aspx for ASP.NET and index.php for PHP.
    Review the browser URL.
    Enable directory browsing to allow listing the contents of the directory.

而下面的 URL 工作正常。

http://localhost:10537/Search?Query=True_IsFixerUpper/True_IsNewConstruction/True_HasHorses/True_HasVirtualTour/True_HasGarage/True_IsShortSale/True_IsWaterFront/True_HasSwimmingPool/True_HasGolfCourse/True_IsWithinGatedCommunity/True_IsMobileManufacturedHome/True_IsForclosure/True_HasFireplace/True_Is55Community/True_IsWaterfrontRiver/True_IsWaterfrontBay/True_IsWaterfrontInteriorCanal/True_IsWaterfrontOcean/True_IsWaterfrontOceanAccess/True_IsWaterfrontIntracoastal/True_IsWaterfrontLake/True_HasViewOcean/True_HasViewGarden/True_HasViewGolfCourse/True_HasViewRiver/True_HasViewCanal/True_HasViewPond/True_HasViewLake/True_HasViewPool/True_HasPhotos/True_IsOpenHouse/True_IsFenced/True_IsNavigable/True_IsAttached/True_IsDetached/True_IsSemiDetached/True_IsOneStory/True_IsTwoStory/True_IsNonMls/True_IsBoatHouse/True_IsBoatSlip/True_IsDockMooring/

http://localhost:10537/Search/True_IsFixerUpper/True_IsNewConstruction/True_HasHorses/True_HasVirtualTour/True_HasGarage

控制器:

public class SearchController : BaseController
{
        public ActionResult Index(string Query)
        {
        }
}

Route

public static void RegisterRoutes(RouteCollection routes)
{
    routes.Ignore("{resource}.axd/{*pathInfo}");
    routes.MapRoute(
        "Search", // Route name
        "Search/{*Query}", // URL with parameters
        new { controller = "Search", action = "Index", Query = UrlParameter.Optional } // Parameter defaults
    );

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );
}

可能是什么问题。


您收到此错误是因为请求中的 URL 段过多。

检查一下:http://www.iis.net/learn/extensions/introduction-to-iis-express/iis-80-express-readme http://www.iis.net/learn/extensions/introduction-to-iis-express/iis-80-express-readme
IIS 8.0 Express returns an HTTP 404.20 error for Too Many URL Segments.


UPDATE:

请按照以下站点更改 URL 分段数的限制。(默认值为 32。)

http://blogs.msdn.com/b/vijaysk/archive/2012/10/11/iis-8-what-s-new-website-settings.aspx http://blogs.msdn.com/b/vijaysk/archive/2012/10/11/iis-8-what-s-new-website-settings.aspx

更改配置后,url 将通过段计数验证。

现在会抛出超过 maxUrlLength 的错误。

您需要添加以下内容到<system.web />你的部分网页配置 file.

<httpRuntime maxUrlLength="9999" maxQueryStringLength="9999" />

然后你的带有很多段的长请求网址终于可以工作了!!!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如果 URL 参数很长,控制器操作不会调用 的相关文章

随机推荐