开发者

asp.net mvc how to manage urls/links and routing centrally (c# + js)

I keep running into problems with URLs and routing.

Couldn't find an answer on SO.

I would like to manage all of my urls/links in a single place.

This is for my C# MVC code and the js/jquery ajax code.

These urls are scattered throughout my application.

Moving to a production server needs some fixes and I don't like the fact that I need to look for all of the occurrences in t开发者_如何学Che application.

I don't mind fixing this once - but I would like to do it only once.

Any ideas how to manage all of these links/urls as a group will be very appreciated.

Be happy ad enjoy life, Julian


Consider using T4MVC

You could use Html.ActionLink or Html.BuildUrlFromExpression(c => c.ControllerAction())


Depends, if you have application reading off certain urls and those urls changed once in a while. then you might want to consider putting all those urls into a database table/etc and retrieve them using specific key.

that way, when your url changed, all you need to do is to change the url on your database and all your application will still be running fine.


Urls should be managed in a single place: the RegisterRoutes static method in Global.asax. In absolutely every other part of your application you should use Html helpers when dealing/generating urls. This way you will never have problems because helpers take into account your routing system.

So instead of writing:

$('#foo').click(function() {
    $('#result').load('/mycontroller/myaction');
    return false;
});

you use an HTML helper to generate this foo:

<%: Html.Action("foo", "myaction", "mycontroller") %>

and then:

$('#foo').click(function() {
    $('#result').load(this.href);
    return false;
});

Never hardcode a single url in your application except of course in global.asax which is the only centralized place urls should be defined. So basically every time you find yourself writing something of the form /foo/bar in some other part than global.asax you are doing it wrong.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜