1

I found this article at asp.net Learn website. I use this article to help me to create an API method to search in the database by email and not id.

However, if you take a look at the article, you will be able to help me fix my problem as follows:

my controller:

[HttpGet]
public string Details(string email);

[HttpGet]
[ActionName("Email")]
public HttpResponseMessage GetEmail(string email);

My WebApiConfig class

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

First, The error I get for the Details method as follows:

(Error 1 'MyApp.Controllers.UsersController.Details(string)' must declare a body because it is not marked abstract, extern, or partial D:\Abdulrhman A A\MyApp\Asp.net Web API\Controllers\UsersController.cs 40 27 MyAppApplication)

Second, the error I get for the GetEmail method as follows:

(Error 2 'MyApp.Controllers.UsersController.GetEmail(string)' must declare a body because it is not marked abstract, extern, or partial D:\Abdulrhman A A\MyApp\Asp.net Web API\Controllers\UsersController.cs 44 43 MyAppApplication)

1 Answer 1

1

The article provides a sample on how to configure routing. These last two samples are just for illustrative purposes, it is more like pseudo-code, not a working code. If you are just starting with C# these last code samples are confusing.

The errors that you are getting clearly describe what is going on - you need a method body. To fix it, instead of adding ; at the end add {} and actually implement the method:

[HttpGet]
public string Details(string email)
{
    // Implement your method, so it returns string
}

[HttpGet]
[ActionName("Email")]
public HttpResponseMessage GetEmail(string email)
{
    // Implement your method so it returns HttpResponseMessage 
}
Sign up to request clarification or add additional context in comments.

3 Comments

Good news, you confirmed my suggestion. I am now working on how to form the method body in case I am searching by email and not id. But I am still struggling in that, can you edit your post with method body please for email search inside the same table?
@AbuTaha It is totally unrelated to your initial question. Instead of asking follow up question in comments (where no one will probably see it), better ask a new question, clearly describing what you want to achieve and what is not working. See the guidelines on how to ask a good question
thank you @dotnetom . I am going to post another question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.