部署后Ajax路由刷新到空白页

时间:2019-07-13 08:27:12

标签: c# ajax asp.net-mvc nginx ubuntu-16.04

在部署后(天蓝色),任何Ajax路由都会使页面空白,在本地开发计算机上完全没问题。在检查元素网络选项卡中,它显示了所需的部分,因此它可以路由至正确的操作/视图,但是将所有页面的html留空,只是尝试了许多不同的方法(例如3天),但还没有遇到任何遗憾格式化,或者如果我没有以正确的方式进行操作,这是我的第一篇文章,如果有人能够帮助或至少指导我朝着这个方向发展,将不胜感激。

我已经尝试过更改路由,以使用ajax jquery方法指定url和get方法,并使用.html(response)而不是.load。尝试在〜/根URL之前添加前缀。使c#方法非异步。不使用数据库查询。不使用Viewbag

.js文件

```
$('#IndexLikes').click(function(){
  $('#Sorting').load(PartialUrls.IndexLikes)
  return false;
})
````

_布局文件

```
<script type="text/javascript">
  var PartialUrls = PartialUrls || {};
  PartialUrls.IndexLikes = '@Url.Action("IndexLikesAsync", "Home")';
</script>
```

控制器

```
[HttpGet("")]
public IActionResult Index()
{
  int? UserId = HttpContext.Session.GetInt32("UserId");
  User User = dbContext.Users.FirstOrDefault(user => user.UserId == UserId);
  List<AdminRecipe> RecipesByRating = dbContext.AdminRecipes.Include(recipe => recipe.Likes).Include(recipe => recipe.Ratings).Include(recipe => recipe.User).ToList();
  ViewBag.RecipesByRating = RecipesByRating.OrderByDescending(recipe => recipe.GetRating());
  ViewBag.User = User;
  return View();
}
[HttpGet("/IndexLikes")]
public async Task<IActionResult> IndexLikesAsync(){
  List<AdminRecipe> RecipesByLikes = await dbContext.AdminRecipes.Include(recipe => recipe.User).Include(recipe => recipe.Ratings).Include(recipe => recipe.Likes).ToListAsync();
  ViewBag.RecipesByLikes = RecipesByLikes.OrderByDescending(recipe => recipe.Likes.Count);
  return View("IndexLikesPartial");
}
```

索引cshtml页面

```
<div class="RecipeList">
  <h2>Here you can find all of our recipes and recipes we choose as the best from the User Submissions</h2>
  <div id="Sorting">
    <table class="SortingTbl">
      <thead class="SortingThead">
        <tr class="SortingTR">
          <td class="SortingBtn" id="IndexRatingReverse"><h4>Sort By Rating ↑</h4></td>
          <td class="SortingBtn" id="IndexLikes"><h4>Sort by Likes ↓</h4></td>
          <td class="SortingBtn" id="IndexTime"><h4>Sort By Newest</h4></td>
        </tr>
      </thead>
    </table>
    @foreach(var recipe in ViewBag.RecipesByRating){
      <div class="Recipe">
        <h3><a asp-action="Recipe" asp-controller="Recipes" asp-route-Title="@recipe.Title">@recipe.Title</a></h3>
        <h5>By @recipe.User.Username</h5>
        @if(recipe.PictureURL != null){
          <div class="RecipeListPicture" style="background-image: url(@recipe.PictureURL)";></div>
        }
        <ul>
          @foreach(var ingredient in recipe.GetIngredients()){
            <li>@ingredient</li>
          }
        </ul>
        <p>@recipe.Content</p>
        <p class="Rating">Rating: @recipe.GetRating()</p>
        <p class="Likes">Likes: @recipe.Likes.Count</p>
        <p class="postDate">Date Posted: @recipe.CreatedAt.ToString("MM/dd/yyyy")</p>
      </div>
    }
</div>
```

IndexLikesPartial cshtml页面

```
<table class="SortingTbl">
  <thead class="SortingThead">
    <tr class="SortingTR">
      <td class="SortingBtn" id="IndexRating"><h4>Sort By Rating ↓</h4></td>
      <td class="SortingBtn" id="IndexLikesReverse"><h4>Sort By Likes ↑</h4></td>
      <td class="SortingBtn" id="IndexTime"><h4>Sort By Newest</h4></td>
    </tr>
  </thead>
</table>
@foreach(var recipe in ViewBag.RecipesByLikes){
   <div class="Recipe">
     <h3><a asp-action="Recipe" asp-controller="Recipes" asp-route-Title="@recipe.Title">@recipe.Title</a></h3>
     <h5>By @recipe.User.Username</h5>
     @if(recipe.PictureURL != null){
       <div class="RecipeListPicture" style="background-image: url(@recipe.PictureURL)";></div>
     }
     <ul>
       @foreach(var ingredient in recipe.GetIngredients()){
         <li>@ingredient</li>
       }
     </ul>
     <p>@recipe.Content</p>
     <p class="Rating">Rating: @recipe.GetRating()</p>
     <p class="Likes">Likes: @recipe.Likes.Count</p>
     <p class="postDate">Date Posted: @recipe.CreatedAt.ToString("MM/dd/yyyy")</p>
     </div>
   }

0 个答案:

没有答案