We help IT Professionals succeed at work.

C# API MVC Issue GET

ITsolutionWizard
on
123 Views
Last Modified: 2019-02-01
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

Comment
Watch Question

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
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

Author

Commented:
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.
Test your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
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>.
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
What does your RouteConfig.cs look like?  And do you have any Route Attributes in the controller?


»bp

Author

Commented:
// Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
CERTIFIED EXPERT

Commented:
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

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
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