开发者

ASP.Net MVC "Magic Strings" - Can they be avoided?

开发者 https://www.devze.com 2022-12-11 00:49 出处:网络
Ok take this example below: public ActionResult ViewProfile() { //Load the profile for the currently logged in user

Ok take this example below:

public ActionResult ViewProfile()
{
    //Load the profile for the currently logged in user
    if (Membership.GetUser开发者_开发技巧() != null)
    {
        //Do some stuff get some data.
        return View(ReturnViewModel);
    }

    return RedirectToAction("MainLogon", "Logon");
}

Is there anyway to avoid the "magic strings" when redirecting to the Logon page?


I would not go near MVC Futures in this case.

I would recommend using T4MVC

David Ebbo talks about it here: http://blogs.msdn.com/davidebb/archive/2009/06/17/a-new-and-improved-asp-net-mvc-t4-template.aspx

With an updated version here also including refactoring support for action methods:

http://blogs.msdn.com/davidebb/archive/2009/06/26/the-mvc-t4-template-is-now-up-on-codeplex-and-it-does-change-your-code-a-bit.aspx

Means that instead of using a literal like this:

<% Html.RenderPartial("DinnerForm"); %>

You can now make use of intellisense and strongly type it:

<% Html.RenderPartial(MVC.Dinners.Views.DinnerForm); %>

This has also been blogged about by Scott Hanselman here:

http://www.hanselman.com/blog/TheWeeklySourceCode43ASPNETMVCAndT4AndNerdDinner.aspx


(1). There is a way to use strongly typed methods. They were once in ASP.NET MVC preview, but were removed from the release and put into MVC Futures

Something like:

Html.ActionLink<HomeController>(c => c.Index(), "Home")

(2). Define constants for all actions and use them.

0

精彩评论

暂无评论...
验证码 换一张
取 消