【问题标题】:No webpage was found for the web address Azurewebsite找不到 Web 地址 Azurewebsite 的网页
【发布时间】:2019-04-18 07:02:59
【问题描述】:

我将应用程序部署到 Azure 网站,在测试应用程序时发现了一个错误。 当我想将产品添加到 Bag 时,Bag 没有显示该产品已添加,当我按下 Bag 图标时,它显示错误

This granihouse.azurewebsites.net page can’t be found No webpage was found for the web address: https://granihouse.azurewebsites.net/Customer/ShoppingCart
HTTP ERROR 404

到目前为止,我所做的是尝试在本地 IIS 上部署应用程序并使用 FileZilla 获取文件并传输到服务器,但没有任何改变。 我所做的第二件事是尝试更改连接字符串并尝试在 Visual Studio 中部署应用程序,但又遇到了同样的问题。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GraniteHouse.Data;
using GraniteHouse.Extensions;
using GraniteHouse.Models;
using GraniteHouse.Models.ViewModel;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace GraniteHouse.Areas.Customer.Controllers
{
    [Area("Customer")]
    public class ShoppingCartController : Controller
    {
        private readonly ApplicationDbContext _db;

        [BindProperty]
        public ShoppingCartViewModel ShoppingCartVM { get; set; }

        public ShoppingCartController(ApplicationDbContext db)
        {
            _db = db;
            ShoppingCartVM = new ShoppingCartViewModel()
            {
                Products = new List<Models.Products>()
            };
        }

        //Get Index Shopping Cart
        public async Task<IActionResult> Index()
        {
            List<int> lstShoppingCart = HttpContext.Session.Get<List<int>>("ssShoppingCart");
            if(lstShoppingCart.Count > 0)
            {
                foreach(int cartItem in lstShoppingCart)
                {
                    Products prod = _db.Products.Include(p => p.SpecialTags).Include(p => p.ProductTypes).Where(p => p.Id == cartItem).FirstOrDefault();
                    ShoppingCartVM.Products.Add(prod);
                }
            }
            return View(ShoppingCartVM);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ActionName("Index")]
        public IActionResult IndexPost()
        {
            List<int> lstCartItem = HttpContext.Session.Get<List<int>>("ssShoppingCart");

            ShoppingCartVM.Appointments.AppointmentDate = ShoppingCartVM.Appointments.AppointmentDate
                                                                        .AddHours(ShoppingCartVM.Appointments.AppointmentTime.Hour)
                                                                        .AddMinutes(ShoppingCartVM.Appointments.AppointmentTime.Minute);
            Appointments appointments = ShoppingCartVM.Appointments;
            _db.Appointments.Add(appointments);
            _db.SaveChanges();

            int appointmentId = appointments.Id;

            foreach (int productId in lstCartItem)
            {
                ProductsSelectedForAppointment productSelectedForAppointment = new ProductsSelectedForAppointment()
                {
                    AppointmentId = appointmentId,
                    ProductId = productId

                };
                _db.ProductsSelectedForAppointment.Add(productSelectedForAppointment);

            }
            _db.SaveChanges();
            lstCartItem = new List<int>();
            HttpContext.Session.Set("ssShoppingCart", lstCartItem);

            return RedirectToAction("AppointmentConfirmation", "ShoppingCart", new { Id = appointmentId});
        }



        public IActionResult Remove(int id)
        {
            List<int> lstCartItem = HttpContext.Session.Get<List<int>>("ssShoppingCart");

            if(lstCartItem.Count > 0)
            {
                if(lstCartItem.Contains(id))
                {
                    lstCartItem.Remove(id);
                }
            }
            HttpContext.Session.Set("ssShoppingCart", lstCartItem);

            return RedirectToAction(nameof(Index));
        }


        //Get
        public IActionResult AppointmentConfirmation(int id)
        {
            ShoppingCartVM.Appointments = _db.Appointments.Where(a => a.Id == id).FirstOrDefault();
            List<ProductsSelectedForAppointment> objProdList = _db.ProductsSelectedForAppointment.Where(p => p.AppointmentId == id).ToList();


            foreach(ProductsSelectedForAppointment prodAtpObj in objProdList)
            {
                ShoppingCartVM.Products.Add(_db.Products.Include(p => p.ProductTypes).Include(p => p.SpecialTags).Where(p => p.Id == prodAtpObj.ProductId).FirstOrDefault());
            }
            return View(ShoppingCartVM);
        }
    }
}

Azure 似乎无法识别 ShoppingCart 控制器。 有什么建议吗?

【问题讨论】:

  • 您是在部署已发布的内容还是源代码?
  • 我的网站内容
  • c-sharpcorner.com/article/… 在此链接中,您可以在底部看到如何部署到 azure
  • 我知道如何发布,但在 Azure 上使用应用程序时出现错误。您认为该错误来自部署?
  • 您发布到 azure 的方式有问题。您可能遗漏了几个步骤

标签: c# asp.net-core azure-web-app-service


【解决方案1】:

如果您的站点在本地运行良好,我会检查您的依赖项是如何配置的。当我访问https://granihouse.azurewebsites.net/ 时,我收到超时错误。假设您在 Visual Studio 中使用右键单击发布,我认为 ApplicationDbContext 指向您的应用服务存在问题。此外,lstShoppingCart 可以为 null,这意味着在检查 .Count 时可能会引发 NullReferenceException,在这种情况下,您的视图将不会被返回。

【讨论】:

  • 当然,但是当我按下 Add to Bag 按钮时,它应该将 Bag 增加到 (1),但这并没有发生,当我按下 Bag View 时它显示 NullReferenceException
  • 我假设 Bag 是 HttpContext.Session.Get&lt;List&lt;int&gt;&gt;("ssShoppingCart");,如果是这样,您如何/何时设置 HttpContext.Session["ssShoppingCart"]
  • 我没有设置它。一旦我运行应用程序,它就可以正常工作,并且在我运行它几次后,我得到运行时错误。 NullReferenceException
  • 如果您没有设置它,那么这就是导致您的 NullReferenceException 的原因。您可以做的一件事是使用空合并 if (lstShoppingCart?.Count &gt; 0),如果 lstShoppingCart 为空,它将返回 false。
  • 是的,我更改了它,现在我可以打开购物车表单 但是我现在注意到的一个想法是 Bag 现在显示项目已添加到 Bag。
猜你喜欢
  • 2013-05-21
  • 2019-11-15
  • 2020-05-05
  • 1970-01-01
  • 2018-09-13
  • 2018-06-11
  • 2018-08-07
  • 1970-01-01
  • 2018-05-31
相关资源
最近更新 更多