【问题标题】:ASP.Net Visual Studio 2017 MVC routing view not opening on Azure/server yet does on IISExpressASP.Net Visual Studio 2017 MVC 路由视图未在 Azure/服务器上打开,但在 IISExpress 上未打开
【发布时间】:2018-02-05 13:55:06
【问题描述】:

我是 ASP.Net MVC 的新手。我搜索并查看了许多答案,但仍然找不到解决方案。

我有一个 ASP.Net MVC 应用程序,它在本地的行为与部署时不同。在部署到顶级 Azure 和其他托管服务提供商时尝试打开“Companies\Index”视图时出现 404 未找到,但在 Visual Studio/IIS Express 中本地运行时它可以工作。

我正在使用初始“​​新应用向导”提供的默认路由。有什么我遗漏的,这可能意味着我在点击 http:\myappname\Companies\Index 或 http:\myname\Companies 时得到 404?

我正在尝试单击以打开新的“公司”视图的操作...

@{
    ViewData["Title"] = "Manage";
}

<h2>Manage</h2>
<p>Choose an item below to configure settings</p>
<div class="">
    <ul>
        <li>@Html.ActionLink("Companies", "Index", "Companies") </li>
    </ul>
</div>

Visual Studio 中应用程序的目录结构...

directory structure

路由...

  public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
        // Set default erorr handler page
        app.UseExceptionHandler("/Error.html");

        var configuration = new ConfigurationBuilder()
            .AddEnvironmentVariables()
            .AddJsonFile(env.ContentRootPath+ "/config.json")
            .Build();

        // Set Developer Exceptions on if in development mode
        if (configuration.GetValue<bool>("FeatureToggles:EnableDeveloperExceptions"))
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");

            });
       }
    }
}

我试图调用控制器索引操作以返回 Companies\Index 视图...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Daylight.Data;
using Daylight.Models;

namespace Daylight.Controllers
{
public class CompaniesController : Controller
{
    private readonly DaylightDataContext _context;

    public CompaniesController(DaylightDataContext context)
    {
        _context = context;
    }

    // GET: Companies
    public async Task<IActionResult> Index()
    {
        return View(await _context.Companies.ToListAsync());
    }

如果有任何指点,我将不胜感激!

【问题讨论】:

  • 您应该复制/粘贴代码并使用四个空格缩进以使其更具可读性。
  • 您已经发布了 Projects 控制器的代码,尽管您询问了 Companies 路线(也许这只是一个错字),但如果不查看正确的代码,我们无能为力。
  • 谢谢你们的cmets,我会照你们的建议做的,谢谢
  • 这是您正在运行的确切代码吗?在我看来是正确的。这意味着如果您在await _context.Companies.ToListAsync() 中遇到错误,我希望得到非 404 响应;但是,如果将其简化,则您的真实代码中可能存在导致我们没有看到的 404 的内容。
  • 非常感谢您查看此内容。这是确切的代码。项目中显然还有其他文件,我只发布了我认为相关的内容。它在 Visual Studio 中运行时确实有效。发布到 Azure 或其他 ISP 时,Company 选项卡返回 404,因此我想到了路由。

标签: asp.net-mvc asp.net-mvc-routing


【解决方案1】:

原来这是由于网站无法访问数据库,因此无法将数据返回到视图,因此视图显示为空白。

【讨论】:

    猜你喜欢
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    • 2017-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多