【发布时间】:2016-01-17 08:13:55
【问题描述】:
我有 localhost:51775/Book/Index/bookname 页面,它可以正常工作。但我想要的是从 url 中删除 Index 并使其显示为 localhost:51775/Book/bookname ,我可以根据需要进行页面链接,但这次 jquery 调用不起作用。
我已经为 routeconfig 添加了一条新路由
routes.MapRoute(
"Book",
"Book/{id}",
new { controller = "Book", action = "Index", id = RouteParameter.Optional }
);
从这个 url 作为http://localhost:51775/Book/bookname 的 Jquery 调用不起作用;
var data = new FormData();
data.append("comment", comment);
data.append("id", id);
var ajaxRequest = $.ajax({
type: "POST",
url: "/Book/AddBookComment",
contentType: false,
processData: false,
data: data
});
但如果我删除 routeconfig 则它的工作方式为 http://localhost:51775/Book/Index/bookname
这是从jquery调用的c#方法;
[HttpPost]
public string AddBookComment()
{
string comment = Request.Form["comment"];
string id = Request.Form["id"];
Int64 idBook = id.ToInt64();
SuggestBusiness.Instance.AddBookComment(idBook, SessionManager.GetSession().CurrentMember.MemberId, comment);
return "ok";
}
localhost:51775/Book/Index/bookname 是工作页面链接,localhost:51775/Book/bookname 是我希望它工作的页面链接,localhost:51775/Book/AddBookComment 是 jquery 的 c# 方法
我应该怎么做才能进行那个 jquery 调用?
【问题讨论】:
-
它路由
RouteConfig文件中的第一个(即在默认路由之前)? -
是的,这是第一个。
-
id参数只能是数字吗?
-
然后显示你的
Index()你发帖的方法。 -
其实我不使用参数我们可以忽略它,因为我从表单数据中发送参数。
标签: jquery asp.net-mvc routes action