1

I have the following method in the Controller and I pass JsonResult to another class (Receiver.cs) in C#. So, what is the proper data type as parameter in Receiver.cs class? I used JsonResult but not sure if it is the best option for this situation? I also return this result from Receiver.cs class to client as Json object.

Controller:

public void Create() {

    //code omitted for brevity
    var data = GetData(); 

    var data = Json(new { Data = data, success = true }, JsonRequestBehavior.AllowGet);

    Receiver.BroadcastData(data); 
}

Here is the method in Receiver.cs:

Receiver.cs:

public class Receiver
{
    public static void BroadcastData(JsonResult data)
    {
        ...
    }
}

Update: Actually I use ASP.NET MVC and SignalR. Here I want to pass Json object from Controller to Hub class. After then, I will broadcast this Json object to the client.

0

1 Answer 1

2

I assume you want to send a JSON object to each SignalR connection.

Using Json() is meant for returning a JSON object from a controller action, so it doesn't really fit here.

But SignalR makes it easy. Any object you send to a SignalR client gets serialized to JSON. So you don't have to worry about the serialization. You don't have to tell it "I want this to be sent as JSON".

So if you want a generic method to send any kind of object to the client, just accept an object. When you pass that off to SignalR, it will serialize it into JSON for the client.

public void Create() {

    //code omitted for brevity
    var data = GetData(); 

    Receiver.BroadcastData(data); 
}
public class Receiver
{
    public static void BroadcastData(object data)
    {
        ...
    }
}

However, I don't entirely know how useful this would be. The client still has to know what kind of data it is so that it can process it in a meaningful way. So you may find that you would actually benefit from different SignalR methods to pass specific types of data to the client, so the client already knows what type of data it's getting.

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

5 Comments

Many thanks for your reply. In this scene I think there is no need to pass data as Json to the Hub class. But I have tried to convert data object to JsonResult before returning to the client, but cannot use Json method as it is used in the Controller. Any idea?
I think returning string to the client is not a good idea as my data has some elements i.e. Total, Sum, Average, etc. For this reason I want to return the data as Json from the Hub.
You don't need to convert the data to JSON at all. SignalR will do that for you.
Here's what the documentation says: "Any data that you receive in parameters or return to the caller is communicated between the client and the server by using JSON, and SignalR handles the binding of complex objects and arrays of objects automatically."
You rock!.. Now it's ok and working like a charm with the help of your valuable hand kind helps... Voted up and marked as answer, thanks a lot :)

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.