This repository was archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
Getting 404 on IRouter implementation #4904
Copy link
Copy link
Closed
Labels
Description
hey guys,
I've been trying to create a custom route, for dynamic URLs basically I just want to pull a list from a database and if there's any coincidence then map the route to a generic controller / action.
Something like:
mydomain.com/dynamic-title-here -> langingPage/Index/10
I've been looking for the documentation but some is either outdated or incomplete, so far I've implemented something like this:
public class LandingPageRouter : IRouter
{
public VirtualPathData GetVirtualPath(VirtualPathContext context)
{
return null;
}
public Task RouteAsync(RouteContext context)
{
var requestPath = context.HttpContext.Request.Path.Value;
if (!string.IsNullOrEmpty(requestPath) && requestPath[0] == '/')
{
requestPath = requestPath.Substring(1);
}
var pagefound = GetPages().Any(x => x == requestPath);
if (pagefound)
{
//TODO: Handle querystrings
var routeData = new RouteData();
routeData.Values["controller"] = "LandingPage";
routeData.Values["action"] = "Index";
context.RouteData = routeData;
}
return Task.FromResult(0);
}
private IEnumerable<string> GetPages()
{
//TODO: pull from database
return new List<string> { "page-url-title", "another-dynamic-url" };
}
}
I'm getting 404 everytime a page is found, I also have tried to instanciate internally a route and return the RouteAsync method from my instace of the route but i'm still getting 404.
My post in SO:
http://stackoverflow.com/questions/37981308/getting-404-on-irouter-implementation-asp-net-core-mvc
any ideas?
Thanks.