Question

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") %>/',
        Delete: '<%= 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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top