I'm trying to navigate to a page which its URL is in the following format: localhost:xxxxx/User/{id}/VerifyEmail?secretKey=xxxxxxxxxxxxxxx
I've added a new route in the RouteConfig.cs
file and so my RouteConfig.cs
looks like this:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "VerifyEmail",
url: "User/{id}/VerifyEmail",
defaults: new { controller = "User", action = "VerifyEmail" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index",
id = UrlParameter.Optional }
);
}
}
Unfortunately, when trying to navigate to that URL I get this page:
<Error>
<Message>
No HTTP resource was found that matches the request URI 'http://localhost:52684/User/f2acc4d0-2e03-4d72-99b6-9b9b85bd661a/VerifyEmail?secretKey=e9bf3924-681c-4afc-a8b0-3fd58eba93fe'.
</Message>
<MessageDetail>
No type was found that matches the controller named 'User'.
</MessageDetail>
</Error>
and here is my UserController:
public class UserController : Controller
{
// GET /User/{id}/VerifyEmail
[HttpGet]
public ActionResult VerifyEmail(string id, string secretKey)
{
try
{
User user = UsersBL.Instance.Verify(id, secretKey);
//logger.Debug(String.Format("User %s just signed-in in by email.",
user.DebugDescription()));
}
catch (Exception e)
{
throw new Exception("Failed", e);
}
return View();
}
}
Please tell me what am I doing wrong?
In my case, the controller was defined as:
public class DocumentAPI : ApiController
{
}
Changing it to the following worked!
public class DocumentAPIController : ApiController
{
}
The class name has to end with Controller!
Edit: As @Corey Alix has suggested, please make sure that the controller has a public access modifier; non-public controllers are ignored by the route handler!
In my case after spending almost 30 minutes trying to fix the problem, I found what was causing it:
My route defined in WebApiConfig.cs
was like this:
config.Routes.MapHttpRoute(
name: "ControllersApi",
routeTemplate: "{controller}/{action}"
);
and it should be like this:
config.Routes.MapHttpRoute(
name: "ControllersApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
as you see it was interfering with the standard route defined in RouteConfig.cs
.
In my case I was using Web API and I did not have the public
defined for my controller class.
Things to check for Web API:
public
: ApiController
Controller
/api/
prefix. eg. 'host:port/api/{controller}/{actionMethod}'If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With