C# 控制器无效时的 StructureMap 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/257772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:14:56  来源:igfitidea点击:

StructureMap error when invalid controller

c#asp.net-mvcstructuremap

提问by Schotime

I am using Structure map like the MVC storefront by Rob Conery does and I have an AdminController and so to get to it I just type:

我正在使用结构图,就像 Rob Conery 的 MVC 店面一样,我有一个 AdminController,所以我只需输入:

website/Admin/action

however if I miss spell the controller name I get the error below:

但是,如果我错过了控制器名称的拼写,则会出现以下错误:

Exception Details: System.ArgumentNullException: Value cannot be null. Parameter name: key

异常详细信息:System.ArgumentNullException:值不能为空。参数名称:key

There error occurs on this line:

此行出现错误:

Controller controller = ObjectFactory.GetInstance(controllerType) as Controller;

Does anyone have any ideas on how I can handle this error or not allow it to happen at all and maybe just goto a 404 page??

有没有人对我如何处理此错误或根本不允许它发生有任何想法,也许只是转到 404 页面?

Cheers in advance

提前干杯

采纳答案by Andrew Stanton-Nurse

The problem is that if there is no controller with the expected type name (i.e. if the user types "Amdin" the ControllerFactory base class will look for "AmdinController" and won't find it, but will still call your overriden method). In that case, the controllerType variable will be null. So you can just check it for null before the line you quoted and then (if it is null) either:

问题是,如果没有具有预期类型名称的控制器(即,如果用户键入“ Amdin”,则 ControllerFactory 基类将查找“ AmdinController”但不会找到它,但仍会调用您的覆盖方法)。在这种情况下,controllerType 变量将为空。因此,您可以在引用的行之前检查它是否为空,然后(如果它为空)或者:

A) Implement a spelling correction such as the one cfeduke suggests

A) 实施拼写更正,如 cfeduke 建议的那样

or B) simply throw an HttpException with the status code 404 (that shouldcause the 404 error you are looking for).

或 B) 简单地抛出一个状态代码为 404 的 HttpException(这应该会导致您正在寻找的 404 错误)。

NOTE: If you do a spelling correction, you should probably do a Response.Redirect to the new URL, rather than just silently loading the right controller, that way the address bar changes to reflect the spelling correction

注意:如果您进行拼写更正,您可能应该对新 URL 执行 Response.Redirect,而不仅仅是静默加载正确的控制器,这样地址栏就会更改以反映拼写更正

回答by cfeduke

You have a couple different options (or if you want, two things you can combine for a solution). To remove some of the potential problems between the chair and address bar you can implement a SoundEx solutionin C#using the new routing framework to potentially capture some misspellings and re-route them to the expected URL (and/or add routes for what you believe common misspellings or requests will be). This, however, isn't a solution that will fully solve the problem so you'll need to look in to implementing custom error pagesfor your application.

您有几个不同的选择(或者,如果您愿意,可以将两件事结合起来作为解决方案)。要消除椅子和地址栏之间的一些潜在问题,您可以使用新的路由框架在 C# 中实现 SoundEx 解决方案以潜在地捕获一些拼写错误并将它们重新路由到预期的 URL(和/或添加您认为的路由)常见的拼写错误或请求将是)。然而,这并不是一个可以完全解决问题的解决方案,因此您需要考虑为您的应用程序实现自定义错误页面