Avatar of ITsolutionWizard
ITsolutionWizard
Flag 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

.NET MVCASP.NETC#

Avatar of undefined
Last Comment
kaufmed

8/22/2022 - Mon
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
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
Bill Prew

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ITsolutionWizard

ASKER
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>.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Bill Prew

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


»bp
ITsolutionWizard

ASKER
// Web API routes
            config.MapHttpAttributeRoutes();

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

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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
kaufmed

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