在ASP.NET MVC中,能否避免使用魔法字符串,实现更健壮的代码?

2026-03-30 12:091阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计285个文字,预计阅读时间需要2分钟。

在ASP.NET MVC中,能否避免使用魔法字符串,实现更健壮的代码?

好的,以下是简化后的内容:

在ASP.NET MVC中,能否避免使用魔法字符串,实现更健壮的代码?

csharppublic ActionResult ViewProfile(){ // 如果当前用户已登录 if (Membership.GetUser() !=null) { // 获取数据 return View(ReturnViewModel); } return RedirectToAction(...);}

好的,请看下面这个例子:

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"); }

在重定向到登录页面时,有没有避免“魔术字符串”?

在这种情况下,我不会接近MVC Futures.

我建议使用T4MVC

David Ebbo在这里谈到它:
blogs.msdn.com/davidebb/archive/2009/06/17/a-new-and-improved-asp-net-mvc-t4-template.aspx

此处的更新版本还包括对操作方法的重构支持:

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

意味着不是使用这样的文字:

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

您现在可以使用intellisense并强烈输入:

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

斯科特·汉塞尔曼(Scott Hanselman)在此发表了一篇博客:

www.hanselman.com/blog/TheWeeklySourceCode43ASPNETMVCAndT4AndNerdDinner.aspx

本文共计285个文字,预计阅读时间需要2分钟。

在ASP.NET MVC中,能否避免使用魔法字符串,实现更健壮的代码?

好的,以下是简化后的内容:

在ASP.NET MVC中,能否避免使用魔法字符串,实现更健壮的代码?

csharppublic ActionResult ViewProfile(){ // 如果当前用户已登录 if (Membership.GetUser() !=null) { // 获取数据 return View(ReturnViewModel); } return RedirectToAction(...);}

好的,请看下面这个例子:

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"); }

在重定向到登录页面时,有没有避免“魔术字符串”?

在这种情况下,我不会接近MVC Futures.

我建议使用T4MVC

David Ebbo在这里谈到它:
blogs.msdn.com/davidebb/archive/2009/06/17/a-new-and-improved-asp-net-mvc-t4-template.aspx

此处的更新版本还包括对操作方法的重构支持:

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

意味着不是使用这样的文字:

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

您现在可以使用intellisense并强烈输入:

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

斯科特·汉塞尔曼(Scott Hanselman)在此发表了一篇博客:

www.hanselman.com/blog/TheWeeklySourceCode43ASPNETMVCAndT4AndNerdDinner.aspx