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

LVL 1
ITsolutionWizardAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
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
ITsolutionWizardAuthor 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.
Bill PrewIT / Software Engineering ConsultantCommented:
public string Get(string Respondent_Name_1, string? test) //api/values?id=1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

ITsolutionWizardAuthor 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 ConsultantCommented:
What does your RouteConfig.cs look like?  And do you have any Route Attributes in the controller?


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

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
Snarf0001Commented:
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

kaufmed   (⌐■_■)Shot Through the Heart, and You're to Blame, You Give vars a Bad NameCommented:
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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET MVC

From novice to tech pro — start learning today.