C# API MVC Issue GET

ITsolutionWizard
ITsolutionWizard used Ask the Experts™
on
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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bill PrewIT / Software Engineering Consultant
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.
IT / Software Engineering Consultant
Top Expert 2016
Commented:
public string Get(string Respondent_Name_1, string? test) //api/values?id=1
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

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 PrewIT / Software Engineering Consultant
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 }
            );
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

ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial