简体   繁体   English

使用ASP.NET MVC创建完全基于Ajax的网站

[英]Creating a fully ajax based website with ASP.NET MVC

What I'm planning is creating a website that is based fully ajax requests. 我正在计划创建一个基于完全ajax请求的网站。 But there are some questions in my mind, 但是我心里有一些疑问,

1- Should I use a javascript routing engine, library ext.? 1-我应该使用JavaScript路由引擎库扩展吗? Do I really need it? 我真的需要吗? Because the website I'm working on is very large and will surely grow up. 因为我正在使用的网站非常大,而且一定会成长。

2- I will just load every page content via ajax, but I won't use JSON , I'm planning to use PartialViews is that OK? 2-我将仅通过ajax加载每个页面内容,但我不会使用JSON ,我打算使用PartialViews可以吗?

3- 3

  <script type="text/javascript">
       $("a.ajax").click(function() {
           $(".placeholder").load(this.href);
           return false;
       });

   </script>
   <a class="ajax" href="/Home/Products/2">Products</a>
   <div class="placeholder"></div>

This is the simple code of my process, the problem is here history, when user wants get back, it will fail. 这是我过程的简单代码,问题在这里,历史记录,当用户想要返回时,它将失败。 How can I achieve this problem? 如何解决这个问题?

1- Should I use a javascript routing engine, library ext.? 1-我应该使用JavaScript路由引擎库扩展吗? Do I really need it? 我真的需要吗? Because the website I'm working on is very large and will surely grow up. 因为我正在使用的网站非常大,而且一定会成长。

If you have lots of logic on the client you might consider using some MVVM javascript framework. 如果您在客户端上有很多逻辑,则可以考虑使用某些MVVM javascript框架。 KnockoutJS and AngularJS are some popular choices. KnockoutJSAngularJS是一些受欢迎的选择。

2- I will just load every page content via ajax, but I won't use JSON, I'm planning to use PartialViews is that OK? 2-我将仅通过ajax加载每个页面内容,但我不会使用JSON,我打算使用PartialViews可以吗?

You could use the HTML5 History API which would allow you to add entries to the browser history everytime you make an AJAX request. 您可以使用HTML5 History API ,该HTML5 History API允许您在每次发出AJAX请求时将条目添加到浏览器历史记录中。

Here's an example: 这是一个例子:

$("a.ajax").click(function() {
    var href = this.href;
    var title = $(this).text();
    $(".placeholder").load(href, function() {
        // Add an entry in the browser history
        history.pushState(null, title, href);
    });
    return false;
});

Try to look on angularjs. 尝试看一下angularjs。 AngularJs + asp.net web api will be the good approach AngularJs + asp.net Web API将是一个好方法

answer q1 : it is better to use some mvvm frameworks like angular.js but also you can handle everything yourself , ofcourse if you want to handle such things you must code much more ! 答案q1:最好使用诸如angular.js之类的mvvm框架,但是您也可以自己处理所有事情,当然,如果要处理这样的事情,则必须编写更多代码! answer q2: it is ok ,simply call an action method via ajax call and in action method render a partialview and in your success function update anywhere you want , I am using this method too :) 答案q2:可以,只需通过ajax调用即可调用一个动作方法,并且在动作方法中呈现一个局部视图,并且在您要成功的函数中更新所需的任何位置,我也正在使用此方法:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM