当您导航到主页时,我有一个电话检索数据库中的所有类别。这个想法是索引页面动态显示类别列表。类别是通过使用EF .Net Core来放置将类别插入数据库的代码来放置的-这就是数据库的外观。
https://gyazo.com/c407f03ae60a82c6fa6f9ee12105089d
编辑:获取类别:
[AllowAnonymous]
public async Task<List<Category>> GetCategoriesFromDb()
{
return await _context.Categories.Where(x => x.ParentId == 0).ToListAsync();
}
它抛出此错误:
https://gyazo.com/7e27dda9ff57b6e28b58b563abd05767
ArgumentNullException: Value cannot be null.
Parameter name: source
System.Linq.Enumerable.Count<TSource>(IEnumerable<TSource> source)
AspNetCore.Views_Home__categories.ExecuteAsync() in _categories.cshtml
+
@if (Model.Count() > 0)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage
page, ViewContext context)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page,
ViewContext context, bool invokeViewStarts)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context) =
AspNetCore.Views_Home_Index.ExecuteAsync() in Index.cshtml
+
<partial name="_categories" , for="Categories" />
似乎在随后导航到页面(字面上刷新一次)后,错误消失了。
这是发生故障的控制器:
https://gyazo.com/ee6db603cde49a7b704b200e7e5fa2dc
// Model stores an IEnumerable of Products and Categories to be used with the two partial views.
Index myModel = new Index();
myModel.Categories = new List<Category>();
myModel.Products = new List<Product>();
myModel.Categories = await GetCategories();
这是代码和有疑问的视图:
https://gyazo.com/634b6867601fc582836e284cdacb0ca3
@{
@model List<Category>
}
<div>
@if (Model.Count() > 0)
{
<a class="btn btn-secondary" href="~/Home/Index/" style="margin-bottom:10px;">All</a>
@foreach (var item in Model)
{
<a class="btn btn-secondary" href="~/Home/Index?Category=@item.Name&Zip=@ViewBag.Zip" style="margin-bottom:10px;">@item.Name</a>
}
}
else
{
<a class="btn btn-secondary" href="~/Home/Index/" style="margin-bottom:10px;">All</a>
}
</div>
这是视图模型:
https://gyazo.com/e5053f6827d2198742279eb37071c733
namespace Market.Views.Home
{
public class Index
{
public List<Product> Products { get; set; }
public List<Category> Categories { get; set; }
}
如果我再次导航至页面,该错误将消失。 恐怕这是我的应用程序返回500错误的0.4%的原因。
我尝试将其从IEnumerable更改为List-但我实际上不知道如何解决该问题。似乎我需要处理类别列表为null的情况,但是我希望视图根本不崩溃,而要等到检索到所有类别。
我可以说是因为当Razor视图点击if语句检查是否有类别时,类别列表为null。我认为应该简单地重试,直到有类别为止,但是我不确定。
编辑: 经过进一步检查,根据Azure,我的SQL数据库服务器上发生了错误。它带有此异常,它似乎与我在本地遇到的问题有关。
System.NullReferenceException: Object reference not set to an instance of an object.
at Market.Controllers.HomeController.GetGeoProducts(Nullable`1 Distance, String productSearch, String Zip, String Category) in C:\Users\nvn\source\repos\market\Market.Core\Market.Core\Controllers\HomeController.cs:line 162
at Market.Controllers.HomeController.Index(String Category, Double Distance, String productSearch, String Zip, String sortOrder, String currentFilter, Nullable`1 pageNumber) in C:\Users\nvn\source\repos\market\Market.Core\Market.Core\Controllers\HomeController.cs:line 80
at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at System.Threading.Tasks.ValueTask`1.get_Result()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
答案 0 :(得分:0)
感谢帮助人员-这最终成为日志记录中的重要课程。
我更改了代码以获取类别,以仅创建预填充的SelectList,而不是从数据库中检索它。
@{
var Categories = new List<Category>(){
//new Category
//{
// CategoryId = 1,
// ParentId = 0,
// Name = "All",
// ParentCategory = null
//},
new Category
{
CategoryId = 2,
ParentId = 0,
Name = "Electronics",
ParentCategory = null
},
new Category
{
CategoryId = 3,
ParentId = 0,
Name = "Furniture",
ParentCategory = null
},
new Category
{
CategoryId = 4,
ParentId = 0,
Name = "Clothing",
ParentCategory = null
},
new Category
{
CategoryId = 5,
ParentId = 0,
Name = "Shoes",
ParentCategory = null
},
new Category
{
CategoryId = 6,
ParentId = 0,
Name = "Video Games",
ParentCategory = null
},
new Category
{
CategoryId = 7,
ParentId = 0,
Name = "Books",
ParentCategory = null
},
new Category
{
CategoryId = 8,
ParentId = 0,
Name = "Tools",
ParentCategory = null
},
new Category
{
CategoryId = 9,
ParentId = 0,
Name = "Vehicles",
ParentCategory = null
},
new Category
{
CategoryId = 10,
ParentId = 0,
Name = "Free",
ParentCategory = null
},
new Category
{
CategoryId = 11,
ParentId = 0,
Name = "Jewelry",
ParentCategory = null
},
new Category
{
CategoryId = 12,
ParentId = 0,
Name = "Crafts",
ParentCategory = null
},
new Category
{
CategoryId = 13,
ParentId = 0,
Name = "Crafts",
ParentCategory = null
},
new Category
{
CategoryId = 14,
ParentId = 0,
Name = "Appliances",
ParentCategory = null
},
new Category
{
CategoryId = 15,
ParentId = 0,
Name = "Health",
ParentCategory = null
},
new Category
{
CategoryId = 16,
ParentId = 0,
Name = "Tickets",
ParentCategory = null
},
new Category
{
CategoryId = 17,
ParentId = 0,
Name = "Services",
ParentCategory = null
},
new Category
{
CategoryId = 18,
ParentId = 0,
Name = "Home & Garden",
ParentCategory = null
},
new Category
{
CategoryId = 19,
ParentId = 0,
Name = "Collectibles",
ParentCategory = null
}
};
但是错误仍然被抛出-所以我在添加NLog之后检查了日志:
2019-05-06 12:09:56.6564|ERROR|Market.Controllers.HomeController|MaxMind.GeoIP2.Exceptions.AddressNotFoundException: The address ::1 is not in the database.
at MaxMind.GeoIP2.DatabaseReader.Execute[T](String ipStr, IPAddress ipAddress, String type, Boolean throwOnNullResponse)
at MaxMind.GeoIP2.DatabaseReader.City(IPAddress ipAddress)
at Market.Controllers.HomeController.GetGeoProducts(Nullable`1 Distance, String productSearch, String Zip, String Category) in C:\Users\nvn\source\repos\market\Market.Core\Market.Core\Controllers\HomeController.cs:line 169
at Market.Controllers.HomeController.Index(String Category, Nullable`1 Distance, String productSearch, String Zip, String sortOrder, String currentFilter, Nullable`1 pageNumber) in C:\Users\nvn\source\repos\market\Market.Core\Market.Core\Controllers\HomeController.cs:line 85
2019-05-06 12:12:07.8714|ERROR|Market.Controllers.HomeController|MaxMind.GeoIP2.Exceptions.AddressNotFoundException: The address ::1 is not in the database.
at MaxMind.GeoIP2.DatabaseReader.Execute[T](String ipStr, IPAddress ipAddress, String type, Boolean throwOnNullResponse)
at MaxMind.GeoIP2.DatabaseReader.City(IPAddress ipAddress)
at Market.Controllers.HomeController.GetGeoProducts(Nullable`1 Distance, String productSearch, String Zip, String Category) in C:\Users\nvn\source\repos\market\Market.Core\Market.Core\Controllers\HomeController.cs:line 169
at Market.Controllers.HomeController.Index(String Category, Nullable`1 Distance, String productSearch, String Zip, String sortOrder, String currentFilter, Nullable`1 pageNumber) in C:\Users\nvn\source\repos\market\Market.Core\Market.Core\Controllers\HomeController.cs:line 85
2019-05-06 12:21:50.0824|ERROR|Market.Controllers.HomeController|MaxMind.GeoIP2.Exceptions.AddressNotFoundException: The address ::1 is not in the database.
at MaxMind.GeoIP2.DatabaseReader.Execute[T](String ipStr, IPAddress ipAddress, String type, Boolean throwOnNullResponse)
at MaxMind.GeoIP2.DatabaseReader.City(IPAddress ipAddress)
at Market.Controllers.HomeController.GetGeoProducts(Nullable`1 Distance, String productSearch, String Zip, String Category) in C:\Users\nvn\source\repos\market\Market.Core\Market.Core\Controllers\HomeController.cs:line 169
at Market.Controllers.HomeController.Index(String Category, Nullable`1 Distance, String productSearch, String Zip, String sortOrder, String currentFilter, Nullable`1 pageNumber) in C:\Users\nvn\source\repos\market\Market.Core\Market.Core\Controllers\HomeController.cs:line 85
由于某种原因,实际错误-在GeoIP DB中找不到IP地址-隐藏在产品中抛出的堆栈跟踪中。不确定为什么,但是要么使用了免费版本中没有的IP范围的人员,要么以在开发模式下进行调试的方式存在一些环回错误。
无论如何,这就是我的处理方式-
// Determine IP address of request.
var ipAddress = HttpContext.Connection.RemoteIpAddress;
try
{
// Grab the city if it's not null
var city = reader.City(ipAddress);
ViewBag.Zip = city.Postal.Code;
ViewBag.CurrentDistance = Distance;
}
catch(AddressNotFoundException e)
{
logger.Warn(e.ToString());
// Grab the city if it's not null
var city = reader.City("insert a random IP here");
ViewBag.Zip = city.Postal.Code;
ViewBag.CurrentDistance = Distance;
}
感谢大家的帮助。