Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

C# API MVC Issue GET

I have the following mvc api and the issue is

when i consume the api like http://localhost:51757/api/Pdf?Respondent_Name_1=ricky&test=me, it works fine.
but
when i consume the api like http://localhost:51757/api/Pdf?Respondent_Name_1=ricky, it alerts The requested resource does not support http method 'GET'.

The only difference is I do not add test parameter value there.

(just FYI, I will have more than 20+ parameters. test is just my example. just to keep it short).


how to fix it?

        [System.Web.Http.HttpGet]
        public string Get(
                
        string Respondent_Name_1, string test 
         
        ) //api/values?id=1
        {
            Domain.DisciplinaryComplaint dd = new Domain.DisciplinaryComplaint();
            MasterController.Service ser = new MasterController.Service();
            dd.respondentName_1= Respondent_Name_1;
            dd.fileExtensionInPDF = ".pdf";
            dd.webpageFileName = "default";
            string returnValue = ser.GetDisciplinaryComplaintDownloadData(dd);
            return returnValue;
        }

Open in new window

Avatar of Bill Prew
Bill Prew

That makes sense since there isn't a method in the controller that matches only one parm coming in.  You have defined it as requiring two, so MVC doesn't find a match to a signature with two parms.

You should be able to make the second parm optional though I believe, and then it should work for both.


»bp
Avatar of ITsolutionWizard

ASKER

bp: you said "You should be able to make the second parm optional though I believe, and then it should work for both."
but how? Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It complaints on visual studio as below:

The type 'string' must be non-nullable value type to use parameter 'T' in the generic type o method Nullable<T>.
What does your RouteConfig.cs look like?  And do you have any Route Attributes in the controller?


»bp
// Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
The MVC routing makes heavier use of default values.
In general I'm not a fan of default parameter values (IMO that's what overloads are for), but in this case adding a null default should do the trick:

public string Get(string Respondent_Name_1, string test = null)

Open in new window


Optionally, the preferred way would be to actually have a model based on ALL of the potential parameters coming in, and then have that as the actual function signature.

public string Get(TestModel model)

public class TestModel
{
	public string Respondant_Name_1 { get; set; }
	public string Test { get; set; }
}

Open in new window

Optionally, the preferred way would be to actually have a model based on ALL of the potential parameters coming in, and then have that as the actual function signature.
While I often prefer that approach myself, the default behavior of MVC is that simple-type parameters are expected to come from the querystring (or the path template), and complex types (i.e. classes) come from the body of the request (most often from a POST). If you're going to use a class to wrap the parameter of a GET, then you need to tell MVC that you're doing that:

public string Get([FromUri]TestModel model)

Open in new window