ASP.NET MVC中的魔术字符串 [英] Magic strings in ASP.NET MVC

查看:114
本文介绍了ASP.NET MVC中的魔术字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有桌面软件开发的背景,并且开始学习ASP.NET MVC.

I have a background in desktop software development and am getting started with learning ASP.NET MVC.

在我的默认HomeController中,我具有Index操作,该操作的代码如下所示:

In my default HomeController I have the Index action which has code that looks like this:

if (!Request.IsAuthenticated)
    return RedirectToAction("Login", "Account");

换句话说,将用户重定向到"/account/login".然后,AccountController.Login操作将处理用户,并在用户成功登录后将其发送回HomeController.

In other words, redirect the user to "/account/login". The AccountController.Login action will then handle the user and send him back to the HomeController once he logs in successfully.

这段代码对我来说很奇怪,也许仅仅是因为我已经习惯了在桌面软件中做不同的事情.如果我将登录操作的名称更改为"LogOn"怎么办?如果我完全删除AccountController并将其替换为其他内容,该怎么办?我将介绍一个新的错误,但不会出现编译器错误,并且我的单元测试也可能不会捕获该错误.由于我使用字符串来指定控制器和动作名称,因此重构和重新设计更有可能在各处破坏代码.

This code smells to me, perhaps just because I'm accustomed to doing things differently in desktop software. What if I change the name of the Login action to "LogOn"? What if I remove the AccountController altogether and replace it with something else? I will introduce a new bug but I won't get compiler errors, and my unit tests probably won't catch it either. Since I used strings to specify controller and action names, refactoring and redesigning has more potential to break code all over the place.

我想要的是这样的

if (!Request.IsAuthenticated)
    return RedirectToAction(() => AccountController.Login);

但是我不确定这是否可能,或者这是否是最好的方法.

However I'm not sure if that's even possible or if it's the best way to do it.

我是愚蠢的,还是其他人遇到了同样的问题?您如何解决这个问题?

Am I being stupid, or have other people had the same problem? What do you do to get around it?

推荐答案

我认为您正在寻找的是

I think what you're looking for is the reason why T4MVC exists - it removes all of the "magic strings" relating to controllers and actions and replaces them with classes and properties.

有了T4MVC,这个

if (!Request.IsAuthenticated)
    return RedirectToAction("Login", "Account");

成为这个

if (!Request.IsAuthenticated)
    return RedirectToAction(MVC.Account.Login());

可以在T4MVC设置中设置一个标志,以强制其在每个版本上运行模板,从而在发生某些更改时向您发出预警.

there is a flag that can be set in the T4MVC settings to force it to run the template on each build, giving you early warning when something might have changed.

虽然不是您要求的内容,但您可以考虑使用

Although not what you asked, you may consider using the AuthorizeAttribute to remove the need to check if the request is authenticated inside of your controller action.

这个

public class HomeController : Controller
{
    public ActionResult Index() 
    {
        if (!Request.IsAuthenticated)
            return RedirectToAction("Login", "Account"); 

        // .... carry on
    }
}

成为

public class HomeController : Controller
{
    [Authorize]
    public ActionResult Index() 
    {
        // .... carry on
    }
}

然后在web.config中,将URL设置为指向帐户登录URL

then in web.config, set the url to point at the account login URL

<authentication mode="Forms">
   <forms loginUrl="account/login" timeout="30" />
</authentication> 

当然,如果您的控制器和操作发生更改(与您的原始投诉类似),这不会给您带来任何安全,但是您始终可以设置一条路线,将所选的URL定向到正确的控制器和操作并使用T4MVC生成的路线中的类,如果情况发生变化,则会向您提供一些编译时警告.

Granted, this doesn't give you any safety if your controllers and actions change (similar to your original complaint), but you can always set up a route to direct the chosen URL to the right controller and action and use the T4MVC generated classes in the route, providing you with some compile time warning if things have changed.

这篇关于ASP.NET MVC中的魔术字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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