1

我会先带来代码。

控制器:

    public ActionResult VideoList(int page)
    {
    }
    [HttpPost]
    public ActionResult SaveWindowWidth(string width)
    {
        return Json(new { success = true });
    }

JS:

$(document).ready(function ()
{
var width = $("#Window").width();

$.ajax(
    {
        type: "POST",
        url: '/Video/SaveWindowWidth',
        async: "true",
        data: { "width": width },
        dataType: 'text json',
        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
        success: function (data, status)
        {
            if (data)
            {
                alert(data.status);
            }
        },
        error: function (data, status)
        {
            if (data)
            {
                alert(data.status);
            }
        }
    })
})

路线:

            routes.MapRoute(
            "Home", // Route name
            "Home", // URL with parameters
            new { controller = "Home", action = "Index" }
        );

        routes.MapRoute(
            "BookPage", // Route name
            "BookPage/{page}", // URL with parameters
            new { controller = "Book", action = "Page" }
        );

        routes.MapRoute(
            "BlogMember", // Route name
            "BlogMember/{id}/{page}", // URL with parameters
            new { controller = "Blog", action = "Member" }
        );

        routes.MapRoute(
            "BlogPost", // Route name
            "BlogPost/{id}/{page}", // URL with parameters
            new { controller = "Blog", action = "Post" }
        );

        routes.MapRoute(
            "BlogPage", // Route name
            "BlogPage/{page}", // URL with parameters
            new { controller = "Blog", action = "Page" }
        );

        routes.MapRoute(
            "BlogTag", // Route name
            "BlogTag/{tag}/{page}", // URL with parameters
            new { controller = "Blog", action = "Tag" }
        );

        routes.MapRoute(
            "NewBlogPostComment", // Route name
            "NewBlogPostComment", // URL with parameters
            new { controller = "Blog", action = "NewBlogPostComment" }
        );

        routes.MapRoute(
            "NewBlogPost", // Route name
            "NewBlogPost", // URL with parameters
            new { controller = "Blog", action = "NewBlogPost" }
        );

        routes.MapRoute(
            "EditBlogPost", // Route name
            "EditBlogPost/{id}", // URL with parameters
            new { controller = "Blog", action = "EditBlogPost" }
        );

        routes.MapRoute(
            "Account", // Route name
            "Account", // URL with parameters
            new { controller = "Account", action = "Update" }
        );

        routes.MapRoute(
            "LogOff", // Route name
            "LogOff", // URL with parameters
            new { controller = "Account", action = "LogOff" }
        );

        routes.MapRoute(
            "LogOn", // Route name
            "LogOn", // URL with parameters
            new { controller = "Account", action = "LogOn" }
        );

        routes.MapRoute(
            "Register", // Route name
            "Register", // URL with parameters
            new { controller = "Account", action = "Register" }
        );

        //routes.MapRoute(
        //    "About", // Route name
        //    "About", // URL with parameters
        //    new { controller = "Home", action = "About" }
        //);

        routes.MapRoute(
            "Contact", // Route name
            "Contact", // URL with parameters
            new { controller = "Home", action = "Contact" }
        );

        routes.MapRoute(
            "UnderConstruction", // Route name
            "UnderConstruction", // URL with parameters
            new { controller = "Home", action = "UnderConstruction" }
        );

        routes.MapRoute(
            "DisableBlogComment", // Route name
            "DisableBlogComment/{id}", // URL with parameters
            new { controller = "Blog", action = "DisableBlogComment" }
        );

        routes.MapRoute(
            "DisableAllMemberBlogComments", // Route name
            "DisableAllMemberBlogComments/{id}", // URL with parameters
            new { controller = "Blog", action = "DisableAllMemberBlogComments" }
        );

        routes.MapRoute(
            "DisableVideoComment", // Route name
            "DisableVideoComment/{id}", // URL with parameters
            new { controller = "Video", action = "DisableVideoComment" }
        );

        routes.MapRoute(
            "DisableAllMemberVideoComments", // Route name
            "DisableAllMemberVideoComments/{id}", // URL with parameters
            new { controller = "Video", action = "DisableAllMemberVideoComments" }
        );

        routes.MapRoute(
            "DisableMember", // Route name
            "DisableMember/{id}", // URL with parameters
            new { controller = "Member", action = "DisableMember" }
        );

        routes.MapRoute(
            "NewPost", // Route name
            "NewPost", // URL with parameters
            new { controller = "Blog", action = "NewPost" }
        );

        routes.MapRoute(
            "InactiveBlogPosts", // Route name
            "InactiveBlogPosts/{page}", // URL with parameters
            new { controller = "Blog", action = "InactiveBlogPosts" }
        );

        routes.MapRoute(
            "InactiveBlogComments", // Route name
            "InactiveBlogComments/{page}", // URL with parameters
            new { controller = "Blog", action = "InactiveBlogComments" }
        );

        routes.MapRoute(
            "InactiveVideoComments", // Route name
            "InactiveVideoComments/{page}", // URL with parameters
            new { controller = "Video", action = "InactiveVideoComments" }
        );

        routes.MapRoute(
            "EditHomePage", // Route name
            "EditHomePage", // URL with parameters
            new { controller = "Home", action = "EditHomePage" }
        );

        routes.MapRoute(
            "EditAboutPage", // Route name
            "EditAboutPage", // URL with parameters
            new { controller = "Home", action = "EditAboutPage" }
        );

        routes.MapRoute(
            "Newsletter", // Route name
            "Newsletter", // URL with parameters
            new { controller = "Newsletter", action = "Newsletter" }
        );

        routes.MapRoute(
            "RegisterNewVideo", // Route name
            "RegisterNewVideo", // URL with parameters
            new { controller = "Video", action = "RegisterNewVideo" }
        );

        routes.MapRoute(
            "Members", // Route name
            "Members/{page}", // URL with parameters
            new { controller = "Member", action = "MemberList" }
        );

        routes.MapRoute(
            "EditMember", // Route name
            "EditMember/{id}", // URL with parameters
            new { controller = "Member", action = "EditMember" }
        );

        routes.MapRoute(
            "AppSettings", // Route name
            "AppSettings", // URL with parameters
            new { controller = "Utility", action = "AppSettings" }
        );

        routes.MapRoute(
            "AudioBookPage", // Route name
            "AudioBookPage/{page}", // URL with parameters
            new { controller = "Book", action = "AudioBookPage" }
        );

        routes.MapRoute(
            "IPBlocked", // Route name
            "IPBlocked", // URL with parameters
            new { controller = "Utility", action = "IPBlocked" }
        );

        routes.MapRoute(
            "LiveTV", // Route name
            "LiveTV", // URL with parameters
            new { controller = "LiveTV", action = "LiveTV" }
        );

        routes.MapRoute(
            "VideoPlayer", // Route name
            "VideoPlayer/{id}/{page}", // URL with parameters
            new { controller = "Video", action = "VideoPlayer" }
        );

        routes.MapRoute(
            "Video", // Route name
            "Video/{page}", // URL with parameters
            new { controller = "Video", action = "VideoList" }
        );

        routes.MapRoute(
            "NewVideoComment", // Route name
            "NewVideoComment", // URL with parameters
            new { controller = "Video", action = "NewVideoComment" }
        );

        routes.MapRoute(
            "Music", // Route name
            "Music", // URL with parameters
            new { controller = "Music", action = "Index" }
        );

        routes.MapRoute(
            "RootsCompilationOne", // Route name
            "Music/RootsCompilationOne", // URL with parameters
            new { controller = "Music", action = "RootsCompilationOne" }
        );

        routes.MapRoute(
            "UnityIsStrength", // Route name
            "Music/UnityIsStrength", // URL with parameters
            new { controller = "Music", action = "UnityIsStrength" }
        );

        routes.MapRoute(
            "FileUpload", // Route name
            "FileUpload", // URL with parameters
            new { controller = "Utility", action = "FileUpload" }
        );

        routes.MapRoute(
            "PageUnavailable", // Route name
            "PageUnavailable", // URL with parameters
            new { controller = "Utility", action = "PageUnavailable" }
        );

        routes.MapRoute(
            "VideoGrid", // Route name
            "VideoGrid/{page}", // URL with parameters
            new { controller = "Video", action = "VideoGrid" }
        );

        routes.MapRoute(
            "EditVideo", // Route name
            "EditVideo/{id}", // URL with parameters
            new { controller = "Video", action = "EditVideo" }
        );

        routes.MapRoute(
            "CopyVideo", // Route name
            "CopyVideo/{id}/{page}", // URL with parameters
            new { controller = "Video", action = "CopyVideo" }
        );

        routes.MapRoute(
            "DeleteIPLogs", // Route name
            "DeleteIPLogs/{ip}", // URL with parameters
            new { controller = "Utility", action = "DeleteIPLogs" }
        );

        routes.MapRoute(
            "SaveWindowWidth", // Route name
            "SaveWindowWidth/{width}", // URL with parameters
            new { controller = "Video", action = "SaveWindowWidth" }
        );

        routes.MapRoute(
            "FacebookLikes", // Route name
            "FacebookLikes", // URL with parameters
            new { controller = "Utility", action = "FacebookLikes" }
        );

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

ajax 尝试调用 VideoList 方法的问题。我在阅读回复时看到它:

参数字典包含方法“System.Web.Mvc.ActionResult VideoList(Int32)”的不可空类型“System.Int32”的参数“页面”的空条目

我在某处听说路线可能有问题,所以我也将它们发布在这里。我会很感激你的帮助

问候马吕斯

4

4 回答 4

3

您的视频路由配置不正确。MVC 尝试从每个 url 获取控制器和操作,以从适当的控制器中找出所需的方法。在您的情况下,它总是获得相同的值 VideoController.VideoList():

routes.MapRoute(
            "Video", // Route name
            "Video/{page}", // URL with parameters
            new { controller = "Video", action = "VideoList" }
        );

如果您将路线更改为此,一切都会好起来的:

routes.MapRoute(
            "Video", // Route name
            "Video/{action}", // URL with parameters
            new { controller = "Video", action = "VideoList" }
        );
于 2012-11-26T20:00:27.220 回答
1

使用字符串作为 URL 存储值不是最佳实践。

我做这样的事情。

看法

@Html.Hidden("getVideoUrl", Url.Action("SaveWindowWidth", "Video"))

JS

   var url = $("#getVideoUrl").val();
    $.post(url, data: { "width": width },      
        success: function (data, status)
        {
            if (data)
            {
                alert(data.status);
            }
        },
        error: function (data, status)
        {
            if (data)
            {
                alert(data.status);
            }
        });
于 2012-11-26T20:05:32.023 回答
1

Dima 已经确定了这个问题,尽管他的解决方案不太适合您。您的路由配置为在请求 url 时Video/SaveWindowWidth,调用VideoList控制器上的操作Video,并将参数page设置为SaveWindowWidth。显然这不是你想要的。

ASP.Net MVC 将执行匹配的第一个路由。这就是为什么首先包含所有特定路线很重要的原因。

解决此问题的方法之一是重写您的路由,以便在您的通用漂亮路由之上配置某些 API 路由。为此,您需要在任何漂亮的 url 之前提及每个组合。

例如,

routes.MapRoute(
        "SaveWindowWidthForVideo", // Route name
        "Video/SaveWindowWidth",
        new { controller = "Video", action = "SaveWindowWidth" }
    );

routes.MapRoute(
        "SaveWindowHeightForVideo", // Route name
        "Video/SaveWindowHeight",
        new { controller = "Video", action = "SaveWindowHeight" }
    );

routes.MapRoute(
        "SaveWindowSomethingElseForVideo", // Route name
        "Video/SaveWindowHeight",
        new { controller = "Video", action = "SaveWindowSomethingElseForVideo" }
    );

// etc etc      

// your remaining pretty urls
routes.MapRoute(
        "Video", // Route name
        "Video/{page}", // URL with parameters
        new { controller = "Video", action = "VideoList" }
    );

我不喜欢这样做,因为它很容易变得相当麻烦。

我更喜欢的解决方案是通过在 url 中添加单词来区分我漂亮的 url 路由和我的 ajax API 路由api

routes.MapRoute(
        "ApiCallsForVideo", // Route name
        "api/video/{action}",
        new { controller = "Video" }
    );

// or

routes.MapRoute(
        "ApiCalls", // Route name
        "api/{controller}/{action}"
    );



// etc etc      

routes.MapRoute(
        "Video", // Route name
        "Video/{page}", // URL with parameters
        new { controller = "Video", action = "VideoList" }
    );

这显然涉及修改您的 ajax 调用以包含单词api.

于 2012-11-26T20:14:41.073 回答
0

嗨,我认为您需要在路由中添加参数,然后只有您的 ajax 会使用参数调用该方法。现在发生的事情是你的 ajax 正在调用那个特定的方法,但你没有在路由中提到允许参数

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

那么只有你的参数可以传递给你的控制器。调用 url 看起来像这样

 url: '/Video/SaveWindowWidth'+width,

或者

url: '/Video/SaveWindowWidth?width=' + width,

它对我来说很好用!

希望能帮助到你

于 2012-11-27T07:21:37.273 回答