1

登録されたルートの 1 つが非常に奇妙な動作をする ASP.NET MVC3 プロジェクト (VS2008 Ultimate) があります。

まず、Global.asax.cs 側から見た様子です。

        public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        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(
            "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 = "IncativeBlogComments" }
        );

        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 = "Send" }
        );

        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 = "Player" }
        );

        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(
            "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(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
    }

次に、関連するコントローラー:

public ActionResult VideoPlayer(int id, int page)
    {
        Video post = VideoProvider.GetPost(id);

        if (post == null)
            return RedirectToAction("PageUnavailable");

        // register view, when user not authenticated Name = "";
        if (SessionData.settings.LogVideoViews)
        {
            VideoProvider.RegisterView(id, Request.UserHostAddress);
        }

        // get comments
        int totalRecords;
        var vVideoComments = VideoProvider.GetPostComments(id, page, SessionData.settings.ListPositionsPerPage, out totalRecords);

        // map to models
        var videoPostModel = Mapper.Map<Video, MVC_Web.Models.VideoModels.VideoListModel>(post);
        var vVideoCommentsModel = Mapper.Map<List<vVideoComment>, List<MVC_Web.Models.VideoModels.vVideoCommentModel>>(vVideoComments);

        ViewBag.postID = id;
        ViewBag.route = "VideoPlayer";
        ViewBag.EnableComments = SessionData.settings.EnableVideoComments;


        ViewBag.selectedMenuItem = "idVideo";
        ViewBag.playerWidth = 512;
        ViewBag.playerHeight = 384;
        ViewBag.comments = vVideoCommentsModel;
        ViewBag.page = page;
        ViewBag.itemsPerPage = SessionData.settings.ListPositionsPerPage;
        ViewBag.totalRecords = totalRecords;


        return View(videoPostModel);
    }

それが、特定のページへのリンクを作成する方法です。

<a href="@Url.Action("VideoPlayer", new { id = Model.ID, page = 1 })">

今まではすべて正常に動作していましたが、ビューがレンダリングされると、ブラウザーのアドレス バーに次のような URL が表示されます。

http://localhost:51408/Video/VideoPlayer/38?page=1

私はむしろ次のようなものを見るべきだったと思います:

http://localhost:51408/VideoPlayer/38/1

このルートを除いて、すべてのルートが正常に機能しています。

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

Phil Haack の Route デバッガーを使用しましたが、URL の正しいルートをキャッチしていることがわかります。

http://localhost:51408/VideoPlayer/38/1

間違ったものを試す場合:

http://localhost:51408/Video/VideoPlayer/38?page=1

リストの一番下からデフォルト ルートを使用して終了します。

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

ここで何が起こっているのかわかりません。この風変わりな URL で長い間 Web サイトを離れていましたが、非常に気になります。助けていただければ幸いです。

ライブで見たい場合: http://www.voiceofconscience.org/Video/VideoPlayer/39?page=1

事前にありがとうマリウス

ps。これは私の最初の Web プロジェクトであり、何か間違ったことをしていると思いますが、何が何なのかわかりません。

4

1 に答える 1

2

Url.Action の代わりに Url.RouteUrl を使用します。

// VideoPlayer is the RouteName listed in the global.asax
Url.RouteUrl("VideoPlayer", new { id = Model.ID, page = 1 });

HTH

于 2012-07-20T20:39:18.337 に答える