0

I am trying to build a generic export operation in mvc. so i wrote a controller for this.

[AcceptVerbs(HttpVerbs.Post)]
public string Excel(FormCollection collection)
{
    string dataUrl = collection["dataUrl"];
    string filter = collection["filter"];
    //Get data from dataUrl
    ...
}

My problem is I want to get data to be transferred from another controller by passing same parameter to its method via POST.

this is the sample method for data

[AcceptVerbs(HttpVerbs.Post)]
public JsonResult List(FormCollection collection)
{
    ...
    return Json(data);
}

Thanks in advance.

Özgür Kara
  • 1,333
  • 10
  • 22
  • Why do you need that? Why not having that logic in another class and call it from both controllers? – Ivo Mar 28 '12 at 15:00
  • i have many controllers and dont want to make changes all of it. – Özgür Kara Mar 28 '12 at 15:03
  • Assuming you are on MVC3, another suggestion (although not 100% related to question) is to stop using FormCollections and use default model binding facility and use real domain objects from your solution. This would facilitate you more to have different service/utility class to do the actual export and have less code in the controller. – Uchitha Mar 28 '12 at 15:05
  • You can Inherit from the other Controller and just call the base method. It's not nice, but you already stated you don't want to refactor it to a better design (btw, your solution is too coupled) – Ivo Mar 28 '12 at 15:22

2 Answers2

1

Why not refactor the export code into a utility class and then use it from both controllers?

Quinton Bernhardt
  • 4,773
  • 19
  • 28
0

Assuming you absolutely have to call controller to controller you can use an HttpWebRequest like so:

public void CallController()
    {
        var request = (HttpWebRequest)WebRequest.Create("http://yoursite.com/Excel");
        request.Method = "POST";

        using (var dataStream = request.GetRequestStream())
        {
            //write your data to the data stream
            using (var response = (HttpWebResponse)request.GetResponse())
            {
                if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.TemporaryRedirect)
                {
                    //work with the controller response
                }
            }
        }
    }

This is very inefficient if you can refactor the code into a class external to both controllers.

BentOnCoding
  • 27,307
  • 14
  • 64
  • 92
  • I tried this already, application use forms authentication so this call give me login pages as response. by the way refactoring code an add another controller method for all data methods is not a good solution for me. – Özgür Kara Mar 29 '12 at 06:56
  • Expose your controller as a post method with no view and no get method. Then you can remove the authentication around your method. – BentOnCoding Mar 29 '12 at 14:50
  • You could do some kind of token security handshake if you were really worried about exposing a controller method with no authentication but, that is outside the scope of this question. – BentOnCoding Mar 29 '12 at 14:51