Asp.Net MVC Ajax URL路由

时间:2016-03-22 14:07:10

标签: asp.net-mvc history.js

我正在开发一个ASP.NET MVC SPA项目,我正在使用Ajax加载不同的页面(partialview)。 一切正常,除了我不能调用包含我的ajaxpage作为参数的URL。

让我说我打电话http://localhost:60452/Events/1我的JavaScript(history.js)将/ info添加为默认页面http://localhost:60452/Events/1/info,这是完全正确的。但是当我在浏览器字段中以相同的方式输入URL时,我得到资源未找到错误。 我的路由可能看起来像:

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

        routes.MapRoute(
            name: "Events",
            url: "{controller}/{id}",
            defaults: new { controller = "Events", action = "Details" }
        );

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

以下是加载部分视图的链接:

 <div id="menucontent" class="dropdown-content">
                        <div>
                            @Ajax.ActionLink("Info", "EventInfo", new {id = "_id_" },
                            new AjaxOptions { HttpMethod = "Get", UpdateTargetId = "pagecontent", OnSuccess = "changePage('', 'info');", OnBegin = "showloader", OnComplete = "hideloader"  },
                            new { @id = "eventinfo" })
                        </div>
                        <div>
                            @Ajax.ActionLink("Start Lists", "Startlists", new { id = "_id_" },
                            new AjaxOptions { HttpMethod = "Get", UpdateTargetId = "pagecontent", OnSuccess = "changePage('Start Lists', 'startlists');", OnBegin = "showloader", OnComplete = "hideloader" },
                            new { @id = "startlists" })
                        </div>
                        <div>
                            @Ajax.ActionLink("Result Lists", "Resultlists", new { id = "_id_" },
                            new AjaxOptions { HttpMethod = "Get", UpdateTargetId = "pagecontent", OnSuccess = "changePage('Result Lists', 'resultlists');", OnBegin = "showloader", OnComplete = "hideloader" },
                            new { @id = "resultlists" })
                        </div>
                        <div>
                            @Ajax.ActionLink("Live", "Live", new { id = "_id_" },
                            new AjaxOptions { HttpMethod = "Get", UpdateTargetId = "pagecontent", OnSuccess = "changePage('Live', 'live');", OnBegin = "showloader", OnComplete = "hideloader" },
                            new { @id = "live" })
                        </div>
                    </div>

我的控制器:

 public class EventsController : Controller
{
    private LiveContext db = new LiveContext();

    public ActionResult Index()
    {
        ViewResult vr = View(db.dbsEvents.ToList());
        return vr;
    }

    public ActionResult Details(int id)
    {
        Event ev = db.dbsEvents.Find(id);
        ViewResult vr = View(ev);
        return vr;
    }

    public ActionResult Startlists(int id)
    {
        Event ev = db.dbsEvents.Find(id);
        return PartialView("_Startlists", ev);
    }

    public ActionResult Resultlists(int id)
    {
        Event ev = db.dbsEvents.Find(id);
        return PartialView("_Resultlists", ev);
    }

    public ActionResult Live(int id)
    {
        Event ev = db.dbsEvents.Find(id);
        return PartialView("_Live", ev);
    }

    public ActionResult EventInfo(int id)
    {
        Event ev = db.dbsEvents.Find(id);
        return PartialView("_EventInfo", ev);
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
            db.Dispose();
        base.Dispose(disposing);
    }
}

我的Javascript:

 var History = window.History;
if (!History.enabled) {
    alert("disabled");
}
History.Adapter.bind(window, 'statechange', function () { // Note: We are using statechange instead of popstate
    var State = History.getState();
});

changePage = function (title, url) {
    $('#title').text(title);
    History.pushState({ page: url }, title, "/Events/" + $.currentevent.Id + "/" + url);
}

我做错了什么或我的路由中丢失了什么?

KR Manuel

0 个答案:

没有答案