English 中文(简体)
为什么很难将URL的扼制成一个MVC的 app子是一种坏的想法? (是吗?)
原标题:Why is Hardcoding URL strings into an MVC app is a bad idea? (Or is it?)

在评论 问题,Phil 提到:

硬通向甚小口径终端探测器是一种坏的想法。 帮助者是出于某种原因。

这样做吗? 如果是,为什么?

最佳回答

乌尔都语的产生是你在全球应用中的路线组合的结果。 当你使用诸如“行动网”之类的助手时,他们检查路线配置,并相应造出路线。

如果你稍后改变其路线组合,或安装虚拟目录,则由助手生成的普通电离层移动式电离层移动式电离层移动式电离层移动式电离层移动式电离层移动式电离层移动式自动电离层移动式电离层移动式电离层移动式电离层移动式电离层移动式电离层移动式电离层移动式电离层移动式电离层机,那么你不必亲自检查和改变每一条硬编码的电离层。

如果你想要标示 anchor,你可以使用。 UrlHelper.Actionmeth。 依靠URL生产的所有帮助者,都使用这种方法。

<a href="@Url.Action("Logon", "Account")">logon<a>
问题回答

我写了一幅小块地方,使URLs难以编码。 我现在倾向于避免这种做法,因为这种做法在你推动生产后立即导致必须改变所有危房。

This was a small webapp, and it still drove me up a wall. I would follow that advice :)

我将不使用乌尔尔·维索(“记录仪”)“Account”(“记录仪”),因为行动名称难以识别。

更好,我们可以像这样写一下延期。

     public static MvcHtmlString ActionLink<TController>(this HtmlHelper helper,
 Expression<Func<TController, object>> expression, string displayText)
            {
                string controller = typeof(TController).GetControllerName();
                string action = expression.GetActionName();

                return helper.ActionLink(displayText, action, controller);
            }

之后,如,

@Html.Action<AccountController>(a=>a.Logon(),"Click here")




相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签