StructureMap错误时,控制无效 [英] StructureMap error when invalid controller

查看:84
本文介绍了StructureMap错误时,控制无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的结构图像罗布科纳MVC的店面确实和我有一个AdminController等得到它我只需键入:

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:

website/Admin/action

但是如果我错过拼控制器名称,我得到以下错误:

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

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

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

在此行中出现的错误有:

There error occurs on this line:

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

有没有人对如何处理此错误,或不允许它发生在所有,也许只是跳转相对404页??任何想法

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??

提前干杯

推荐答案

的问题是,是否存在与预期的类型名称的控制器(即如果用户键入 Amdin 中的ControllerFactory基地类将寻找 Amdin 控制器,将无法找到它,但还是会打电话给你的覆盖方法)。在这种情况下,controllerType变量将为空。所以,你可以检查它的报价你前行空,然后(如果为空)之一:

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:

A)实现一个拼写校正,如一个cfeduke提示

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

或B)简单地抛出了状态code 404(即的HttpException应的导致404错误,您正在寻找)。

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

请注意:如果你做了拼写校正,你应该做的Response.Redirect到新的URL,而不是只是默默地加载正确的控制器,这样地址栏的变化反映拼写校正

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

这篇关于StructureMap错误时,控制无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆