1

I'm not sure how to search for what I'm looking for. All search terms that I attempt seem to return totally irrelevant results. So my apologies in advance if this is a dumb question, but if this is possible it seems like it would make some things much more efficient. I've inherited my first MVC website just over a year ago & I get around, but I still have much to learn.

When the user hits a URL in the browser, the "OnActionExecuting" method is called for the (CS) Controller and it does all of the verification of the user, permissions, etc. (I'm trying to keep this simple, but if you need more info, let me know) Then the "public ActionResult Index()" is called and it loads the CSHTML files and displays them to the user. At this point the CSHTML Page has access to some limited data that has been passed back for "ViewBag" (not sure if that is a common name for it, or setup by the previous programmer) settings.

Then the page calls it's (JavaScript) Controller. From what I've read, the JavaScript has no access to the "ViewBag" data or anything from the server at this point. The JS is then calling AJAX methods which exist back in the exact same (CS) Controller. It once again does all the exact same user/permission checks, then retrieves whatever data it is trying to retrieve.

So I guess my question is, is there either a way to pass back "every page" JSON data (like the user's name) so that it doesn't have to be retrieved, or is this doing something out of order by doing the initial data checks in the 1st place, or what? I abhor that so much functionality gets called multiple times to do the exact same thing. In fact, I haven't been testing this in this iteration, but if there were multiple AJAX called on every page (other than they should be merged if possible), it would perform the user/permission checks once per AJAX call.

Can someone help me understand what I am missing, please?

8
  • Can you please be concise on your essay? Commented Apr 13, 2017 at 17:48
  • @MuhammadRamzan Last time I asked a question, I didn't provide enough info and people dinged my reputation for it. I just can't win around here. Commented Apr 13, 2017 at 17:51
  • Do you want your json data should be retrieved from server only one time or you want security checks to be removed from your json data request? Commented Apr 13, 2017 at 17:58
  • @MuhammadRamzan When the user goes to one specific page, it checks who they are, then it retrieves who they are via AJAX, and then it pulls the primary set of data (via AJAX). That is 3 times it verifies who the user is every time they go to this one page. I feel like there has to be a way to eliminate the need for the AJAX and simply return the data the 1st time. If future AJAX calls are needed after that (on button click or whatever), then of course it should check the user at that point. Commented Apr 13, 2017 at 18:05
  • I suggest you to make a simple example with one controller and view and share code and request calls status image using Fiddler or development tools of chrome or Mozilla and post new Question Commented Apr 13, 2017 at 18:19

1 Answer 1

0

A workaround for your scenario is the following:-

  1. Pass your common "every page" data to .ViewBag objects in your OnActionExecuting()

  2. On your " Views \ Shared \ _Layout.cshtml" create a javascript block and write your JavaScript in a similar fashion:

    <script>
        var username = "@ViewBag.Username";
    </script>
    

When your .cshtml is being rendered, the razor call of @ViewBag.Username will retrieve whatever is saved inside that object and will fill it within the quotation, hence becoming part of your JavaScript before arriving to your Browser.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.