开发者

.net MVC, dealing with json and Areas

开发者 https://www.devze.com 2022-12-22 23:08 出处:网络
When not using areas dealing with the url for the JsonResults from the clientside is not much of a pain, usually you can get away with hardcoding the url into your json calls and get away with it. Pro

When not using areas dealing with the url for the JsonResults from the clientside is not much of a pain, usually you can get away with hardcoding the url into your json calls and get away with it. Problem arises when you have different areas and you don't want to update all your links when you move a controller (and its views) from one area to the other. I'm not very satisfied with the way I'm handling this scenario by having this chunk of code inside every view:

<script type="text/javascript">
    var ControllerActions = {
        JsonSearch: '<%= Url.Action("JsonSearch") %>/',
        JsonDelete: '<%= Url.Action("JsonDelete") %>/',
        Edit: '<%= Url.Action("Edit") %>/',
        Del开发者_如何学JAVAete: '<%= Url.Action("Delete") %>/'
    }
</script>

While it does work no matter where I place the views/controllers I wonder if there's a more elegant to do this.


I personally like the approach you've taken above. Ultimately, if you want to have mobility of your controllers and actions, the best thing is to depend on the Route generators availble in ASP.NET MVC (such as your Url.Action). My only other recommendation is to pull it into a partial/reusable control so it boils down to a single line in your code.

0

精彩评论

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

关注公众号