Passing session object to angularjs function in razor view MVC - asp.net

I need to pass the asp.net session object to a function that I call in ng-init.
something like this;
ng-init=initUser( #((User)Session["userSession"]) )
I just want to pass my "User" object to the initUser method declared in angularjs. Is there a way to do it ?.
I have searched for a solution and all are suggested to call an action method in the controller inside the "initUser" method and fetch the session object from the server and assign it to angularjs scope variable.
Thanks.

The solution you searched and got as the response is more elegant since the front-end would query the data from the server and serve it through an API.
However if you want to inject the .NET session to javascript you could Json encode it to a variable and add it to the scope.
var json = #Html.Raw(Json.Encode(#((User)Session["userSession"]));

Related

Is there a way in ASP.net to fetch data from database using Jquery(not using Webservices/WebMethods)

Is there a way in ASP.net to fetch data from database using Jquery Ajax Method. I know that i can easily access the data by creating a webservice and get it from there using Ajax.
What I want to know is that how can I get the data from simple class methods which accesses the database rather than creating a webservice.
One way can be is to write a HTTPHandler and call that handler using Jquery to retrieve the data.
Check this out for a super-easy (lazy) way of doing a return straight from a public method defined in your aspx.cs file:
asp.net web forms json return result
Example shows putting [WebMethod] and [ScriptMethod(ResponseFormat = ResponseFormat.Json)] attributes on top of the method allows you to call directly from ajax client side request. Worth considering securing this method though to prevent calls from elsewhere!

Formatting uri template string values in ASP.NET WebApi

I have an ASP.NET WebApi application that has some controller methods that expect certain strings to be passed in as method parameters (declared as part of the route template).
On all the methods, the strings passed in are base64-encoded -- which means each controller method must base64-decode them before doing anything with them. While I can obviously have each method do this easily enough, I was wondering if there was a way to perform the decoding before the string actually gets passed to the controller method. I presume this is something along the lines of an action filter or custom formatter, but I'm not familiar enough with asp.net web api to know where to start on that?
Summary:
I've got route templates like : {controller}/{encodedString}/whatever
where {encodedString} is always a base64-encoded string.
and controllers with methods like
GetWhatever(string encodedString)
{
Base64Decode(encodedString);
// do other stuff...
}
I would like to use some part of the asp.net webapi pipeline to decode {encodedString} before the controller method is actually called. What path should I start down in order to do this?
You can create a custom model binder and attach it to the parameters using the ModelBinderAttribute. In the model binder you then do the base64 decoding.
For a reference on parameter binding in Web API check:
How WebAPI does Parameter Binding

How can I pass parameters to an Action using Html.Action() in ASP.NET MVC?

I've been using Html.Action("ActionName", "ControllerName") to invoke child actions across controllers without needing to have the view in Views\Shared. This has been working great for displaying things like session or cookie information.
Instead of just accessing cookies, I would like to pass additional parameters to Html.Action("ActionName", "ControllerName") so the action can execute different code based on the the data passed to the original view.
Should I be using a different method to pass parameters to a child action in a different controller? How would one accomplish this?
You could specify additional data in the RouteValues property like this.
Html.Action("ActionName","Controller", new { id = 1 })
To add a little on this question, I am using ASP.Net MVC 5 and I could succeed to achieve this with this code:
#Html.Action("foo",new {parameter1=1})

Accessing ASP.NET Server variables in jQuery

In Page_Load() function, I have populated an arraylist and a hashtable. I need to read their values in the $(document).ready(function(){...}.
I am a beginner in terms of ASP.NET and jQuery. Please suggest a way to do it.
cheers
you can use PageMethods to make a call to your aspx page from JS. Here is a nice article on how to use page method with jQuery. You can return your "whatever" in your page method. If you want to serialize your server side objects before passing them to the client side, you can serialize them to JSON . Here is an example. You would just read your json data after your ajax call is successful("success: function(msg){//do something}") and update your HTML accordingly
you can use Page.ClientScript property to register arrays, scripts etc.
For more information view MSDN
you can create a page.method in server side and call it in the client side script that return an array of values
by adding
<scriptManager EnablePageMethods="true">
in the server side you create a function that return an array of values
ex: GetArrayResult()
if you intersting in this i will give you the rest of example
JSON.Net will help you serialize the hashtable and array to JSON, and you can store these in a hidden field. Then on the client side you can deserialize the strings to your javascript variables with:
var array = JSON.parse($('#hiddenTextField').val());

pass parameters to HTTPService and use them inside the URL

Flex3 + Cairngorm. I have my service in Servicis.mxml:
<mx:HTTPService id="docIndex" url="{URL_PREFIX}/jobs/{???}/docs" resultFormat="e4x"/>
And I call it from my generic restful delegate like this:
public function index(params:Object):void {
var call:AsyncToken = services.getHTTPService(resourceName+"Index").send(params);
call.addResponder(responder);
}
I want to know how I can use the params Object I pass inside the url definition (the ??? above). And please tell me how you would go about searching an answer to this in the documentation, I'd like to be a little more independet on these problems...
EDIT: I'll explain myself if you didn't understand my problem:
I have a restful api written in rails to which I'm connecting. Doc is a child resource of Job. If I want to get all docs I have to supply a job_id too. Therefore in the service the url must be changed for each .send() call, with the proper job_id (the ??? part above). I'd like to call it like myDelegate.index({job_id:34}) and insert that job_id field in the Service URL.
Write a class that extends HTTPService and allows you to set parameters into the url. Then, in your index function you can fetch it with services.getHTTPService, and call a function you create that sets the url values for you.
In your service locator create an instance of your class rather than a flat HTTPService.

Resources