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)' の null 非許容型 'System.Int32' のパラメーター 'page' の null エントリが含まれています。

ルートに問題があるのではないかとどこかで聞いたので、こちらにも載せておきます。助けていただければ幸いです

よろしくマリウス

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

Dima がこの問題を特定しましたが、彼の解決策はあなたにはうまくいきません。url をリクエストするときに、パラメーターを に設定してコントローラーでVideo/SaveWindowWidthアクションを呼び出すように、ルートが構成されています。明らかに、それはあなたが望んでいたものではありません。VideoListVideopageSaveWindowWidth

ASP.Net MVC は、一致する最初のルートを実行します。そのため、最初にすべての特定のルートを含めることが重要です。

これを修正する方法の 1 つは、特定の 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 に答える
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 に答える
0

こんにちは、ルートにパラメーターを追加する必要があると思います。その後、ajax のみがパラメーターを使用してそのメソッドを呼び出します。今起こっていることは、あなたの ajax がその特定のメソットを呼び出しているということですが、パラメータを許可するためにルートで言及していません

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

その場合、ur パラメーターのみを ur コントローラーに渡すことができます。呼び出し元の URL は次のようになります

 url: '/Video/SaveWindowWidth'+width,

また

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

私にとってはうまくいきます!

それが役に立てば幸い

于 2012-11-27T07:21:37.273 に答える