【问题标题】:creating canonical url with using AJAX MVC使用 AJAX MVC 创建规范 url
【发布时间】:2014-05-10 20:56:05
【问题描述】:

我有这种方式的网址-

Home/Index/?Id=234&videotitle=testtitle

我想把这个网址转换成规范的网址,像这样-

Home/Index/234/testtitle

场景-

 @for (int i = 0; i < Model.Count(); i++)
        {

    <a href="javascript:;" id="video-@Model[i].vd_Id">@Model[i].videoname</a>


    $(function () {
            $('#video-@Model[i].vd_Id').click(function () {

                $.ajax({
                    url: '/Home/VideoInfo/?vd_Id=@Model[i].vd_Id' + '&videoview=' + '@Model[i].Video_view',
                    type: 'post',
                    success: function (data) {
                        window.location.href = "/Home/Video/ +'" + data.idofthisvideo + "'" + "/" + data.titleofthisvideo
                    }
                });
            });

        });

    </script>

}

当我点击锚点时,它转到方法-

  public JsonResult VideoInfo(long? vd_Id, long? videoview)
        {
            var dataa = (from u in db.Channels
                         select new Addvideo
                        {
                            videoname = dataa.videoname,
                            title = dataa.title,
                            vd_Id = Convert.ToInt64(dataa.vd_Id)
                        }).SingleOrDefault();

            var idofvideo = list.vd_Id;
            var titleofvideo = list.title;
            return Json(new { success = true, viewdata = list, idofthisvideo = idofvideo, titleofthisvideo = titleofvideo });

        }

注意

我正在通过 Json 结果取回 Id 和 title,然后尝试以某种方式更改 url-

success: function (data) {
                            window.location.href = "/Home/Video/ +'" + data.idofthisvideo + "'" + "/" + data.titleofthisvideo
                        }

因为这调用了另一个方法-

 public ActionResult Video(long? vd_Id)
          {
            .
            .
            .
            .
            .
          return view(someview); 

           }

然后它失败并出现服务器错误和这个 url-

/Home/Video/%20+'234'/Test%20title

HTTP Error 404.11 - Not Found
The request filtering module is configured to deny a request that contains a double escape sequence.

如何将此网址转换为正确的规范网址?

【问题讨论】:

  • 需要在RoutConfig文件中设置Url Routes

标签: jquery asp.net-mvc url-rewriting


【解决方案1】:

在 asp.net mvc 4 项目的 App_Start 目录中的 Url 路由(RouteConfig 类)中,添加如下操作:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 2011-12-09
    相关资源
    最近更新 更多