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?

Bryan
  • 155
  • 2
  • 12
  • Can you please be concise on your essay? – Muhammad Ramzan Apr 13 '17 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. – Bryan Apr 13 '17 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? – Muhammad Ramzan Apr 13 '17 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. – Bryan Apr 13 '17 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 – Muhammad Ramzan Apr 13 '17 at 18:19
  • @MuhammadRamzan Sorry, I know specific code examples help & I'll try to do that if I get a chance. But I've already spent more time than I'd prefer trying to get the flow down to something more simple. I figure someone has covered this exact thing before, but I don't know what search terms would help me find it. – Bryan Apr 13 '17 at 18:25
  • You need Cookie Authentication.... Maybe this will help you http://stackoverflow.com/questions/30372022/authorization-with-session-variables-in-asp-net-mvc-5.... – Muhammad Ramzan Apr 13 '17 at 18:35
  • See if this helps http://stackoverflow.com/a/41312348/2592042 – Rajshekar Reddy Apr 17 '17 at 05:37

1 Answers1

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.

Manaf
  • 41
  • 7