I am trying to build my own little cms. I created an abstract pageBase class that is inherited by Static, Reviews, Articles, News. Each having there own controller methods.
My problem is that I need to allow the admin to define his own custom path levels. E.g. news\local\mynewdog or Articles\events\conventions\mycon. So I would like a way to pass an array of strings and also set the custom routing.
You can make CMS-style routes seamlessly with a custom RouteBase subclass.
public class PageInfo
{
// VirtualPath should not have a leading slash
// example: events/conventions/mycon
public string VirtualPath { get; set; }
public Guid Id { get; set; }
}
public class CustomPageRoute
: RouteBase
{
private object synclock = new object();
public override RouteData GetRouteData(HttpContextBase httpContext)
{
RouteData result = null;
// Trim the leading slash
var path = httpContext.Request.Path.Substring(1);
// Get the page that matches.
var page = GetPageList(httpContext)
.Where(x => x.VirtualPath.Equals(path))
.FirstOrDefault();
if (page != null)
{
result = new RouteData(this, new MvcRouteHandler());
// Optional - make query string values into route values.
this.AddQueryStringParametersToRouteData(result, httpContext);
// TODO: You might want to use the page object (from the database) to
// get both the controller and action, and possibly even an area.
// Alternatively, you could create a route for each table and hard-code
// this information.
result.Values["controller"] = "CustomPage";
result.Values["action"] = "Details";
// This will be the primary key of the database row.
// It might be an integer or a GUID.
result.Values["id"] = page.Id;
}
// IMPORTANT: Always return null if there is no match.
// This tells .NET routing to check the next route that is registered.
return result;
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
VirtualPathData result = null;
PageInfo page = null;
// Get all of the pages from the cache.
var pages = GetPageList(requestContext.HttpContext);
if (TryFindMatch(pages, values, out page))
{
if (!string.IsNullOrEmpty(page.VirtualPath))
{
result = new VirtualPathData(this, page.VirtualPath);
}
}
// IMPORTANT: Always return null if there is no match.
// This tells .NET routing to check the next route that is registered.
return result;
}
private bool TryFindMatch(IEnumerable<PageInfo> pages, RouteValueDictionary values, out PageInfo page)
{
page = null;
Guid id = Guid.Empty;
// This example uses a GUID for an id. If it cannot be parsed,
// we just skip it.
if (!Guid.TryParse(Convert.ToString(values["id"]), out id))
{
return false;
}
var controller = Convert.ToString(values["controller"]);
var action = Convert.ToString(values["action"]);
// The logic here should be the inverse of the logic in
// GetRouteData(). So, we match the same controller, action, and id.
// If we had additional route values there, we would take them all
// into consideration during this step.
if (action == "Details" && controller == "CustomPage")
{
page = pages
.Where(x => x.Id.Equals(id))
.FirstOrDefault();
if (page != null)
{
return true;
}
}
return false;
}
private void AddQueryStringParametersToRouteData(RouteData routeData, HttpContextBase httpContext)
{
var queryString = httpContext.Request.QueryString;
if (queryString.Keys.Count > 0)
{
foreach (var key in queryString.AllKeys)
{
routeData.Values[key] = queryString[key];
}
}
}
private IEnumerable<PageInfo> GetPageList(HttpContextBase httpContext)
{
string key = "__CustomPageList";
var pages = httpContext.Cache[key];
if (pages == null)
{
lock(synclock)
{
pages = httpContext.Cache[key];
if (pages == null)
{
// TODO: Retrieve the list of PageInfo objects from the database here.
pages = new List<PageInfo>()
{
new PageInfo()
{
Id = new Guid("cfea37e8-657a-43ff-b73c-5df191bad7c9"),
VirtualPath = "somecategory/somesubcategory/content1"
},
new PageInfo()
{
Id = new Guid("9a19078b-2d7e-4fc6-ae1d-3e76f8be46e5"),
VirtualPath = "somecategory/somesubcategory/content2"
},
new PageInfo()
{
Id = new Guid("31d4ea88-aff3-452d-b1c0-fa5e139dcce5"),
VirtualPath = "somecategory/somesubcategory/content3"
}
};
httpContext.Cache.Insert(
key: key,
value: pages,
dependencies: null,
absoluteExpiration: System.Web.Caching.Cache.NoAbsoluteExpiration,
slidingExpiration: TimeSpan.FromMinutes(15),
priority: System.Web.Caching.CacheItemPriority.NotRemovable,
onRemoveCallback: null);
}
}
}
return (IEnumerable<PageInfo>)pages;
}
}
You can register the route with MVC like this.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Case sensitive lowercase URLs are faster.
// If you want to use case insensitive URLs, you need to
// adjust the matching code in the `Equals` method of the CustomPageRoute.
routes.LowercaseUrls = true;
routes.Add(
name: "CustomPage",
item: new CustomPageRoute());
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The above assumes you have a CustomPageController with a Details action method.
public class CustomPageController : Controller
{
public ActionResult Details(Guid id)
{
// Do something with id
return View();
}
}
You can change the route if you want it to go to a different controller action (or even make them constructor parameters).
Related
In the old .Net Framework MVC implementations, I was creating routes by myself so that I could also influence urls generation. Part of the code:
public class RouteBase : Route
{
public RouteBase(string url, IRouteHandler routeHandler) : base(url, routeHandler) { }
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
if (Url.Contains("{segment}") && !values.ContainsKey("segment"))
values["segment"] = requestContext.HttpContext.Items["segmentValue"];
return base.GetVirtualPath(requestContext, values);
}
}
Thanks to GetVirtualPath, I was able to detect a particular segment in the route template and inject a proper value in the route values dictionary so that the client app did not have to specify it when calling for instance Url.RouteUrl(routeName).
In asp.net core 6, I'm now using attributes based routing and I don't know how to hook into this so that I can inject some value into the route values dictionary when I generate urls. If I have a route template like so:
[Route("{segment}/test", Name = "name"]
When I call this, I want an injection mechanism from somewhere else in the code so that the known segment value is injected into the route values used to build the url:
var url = Url.RouteUrl("name"); // Not passing new { segment = value } as second param
For information, I simply use this in Startup:
app.MapControllers();
You can create and register a custom UrlHelper. It will give you the ability to manipulate the behavior as per your use case:
public class CustomUrlHelper : UrlHelper
{
public CustomUrlHelper(ActionContext actionContext)
: base(actionContext) { }
public override string? RouteUrl(UrlRouteContext routeContext)
{
// if(routeContext.RouteName == "name" && routeContext.Values....)
// routeContext.Values = ....
return base.RouteUrl(routeContext);
}
}
public class CustomUrlHelperFactory : IUrlHelperFactory
{
public IUrlHelper GetUrlHelper(ActionContext context)
{
return new CustomUrlHelper(context);
}
}
and in your Program.cs:
builder.Services.AddSingleton<IUrlHelperFactory, CustomUrlHelperFactory>();
Then by calling the Url.RouteUrl("name"), your CustomUrlHelper will be called.
Amir's answer put me on track to find a solution (bounty award for him). Creating a custom UrlHelper was the way to go, but not with a UrlHelper derived class. For enpoint routing, the framework is using the sealed EndpointRoutingUrlHelper class. So I just needed to derive from UrlHelperBase, paste the code from EndpointRoutingUrlHelper and add my customizations. I was lucky that there were no internal pieces of code in there...
Here is the solution. Note that:
the term "segment" mentioned in the original question is replaced by what I actually have in my code i.e. "lang".
HttpContext.Items["lang"] is set by a middleware.
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Mvc;
// The custom UrlHelper is registered with serviceCollection.AddSingleton<IUrlHelperFactory, LanguageAwareUrlHelperFactory>();
public class LanguageAwareUrlHelperFactory : IUrlHelperFactory
{
private readonly LinkGenerator _linkGenerator;
public LanguageAwareUrlHelperFactory(LinkGenerator linkGenerator)
{
_linkGenerator = linkGenerator;
}
public IUrlHelper GetUrlHelper(ActionContext context)
{
return new LanguageAwareUrlHelper(context, _linkGenerator);
}
}
// Source code is taken from https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.Core/src/Routing/EndpointRoutingUrlHelper.cs
// and modified to inject the desired route value
public class LanguageAwareUrlHelper : UrlHelperBase
{
private readonly LinkGenerator _linkGenerator;
public LanguageAwareUrlHelper(ActionContext actionContext, LinkGenerator linkGenerator) : base(actionContext)
{
if (linkGenerator == null)
throw new ArgumentNullException(nameof(linkGenerator));
_linkGenerator = linkGenerator;
}
public override string? Action(UrlActionContext urlActionContext)
{
if (urlActionContext == null)
throw new ArgumentNullException(nameof(urlActionContext));
var values = GetValuesDictionary(urlActionContext.Values);
if (urlActionContext.Action == null)
{
if (!values.ContainsKey("action") && AmbientValues.TryGetValue("action", out var action))
values["action"] = action;
}
else
values["action"] = urlActionContext.Action;
if (urlActionContext.Controller == null)
{
if (!values.ContainsKey("controller") && AmbientValues.TryGetValue("controller", out var controller))
values["controller"] = controller;
}
else
values["controller"] = urlActionContext.Controller;
if (!values.ContainsKey("lang") && ActionContext.HttpContext.Items.ContainsKey("lang"))
values["lang"] = ActionContext.HttpContext.Items["lang"];
var path = _linkGenerator.GetPathByRouteValues(
ActionContext.HttpContext,
routeName: null,
values,
fragment: urlActionContext.Fragment == null ? FragmentString.Empty : new FragmentString("#" + urlActionContext.Fragment));
return GenerateUrl(urlActionContext.Protocol, urlActionContext.Host, path);
}
public override string? RouteUrl(UrlRouteContext routeContext)
{
if (routeContext == null)
throw new ArgumentNullException(nameof(routeContext));
var langRouteValues = GetValuesDictionary(routeContext.Values);
if (!langRouteValues.ContainsKey("lang") && ActionContext.HttpContext.Items.ContainsKey("lang"))
langRouteValues.Add("lang", ActionContext.HttpContext.Items["lang"]);
var path = _linkGenerator.GetPathByRouteValues(
ActionContext.HttpContext,
routeContext.RouteName,
langRouteValues,
fragment: routeContext.Fragment == null ? FragmentString.Empty : new FragmentString("#" + routeContext.Fragment));
return GenerateUrl(routeContext.Protocol, routeContext.Host, path);
}
}
In Asp.Net Core, I use the below two methods and it is able to successfully generate the URL.
[Route("{segment}/test", Name = "name"]
var url1 = Url.RouteUrl("name", new { segment = "aa" });
var url2 = Url.Action("Action", "Controller", new { segment = "aa" });
I have written an odata v3 web api and calling it but it throws error.
The OData path is invalid.
Invalid action detected. 'Get' is not an action that can bind to 'Collection([EPICOR.Models.TasksList Nullable=False])'.
Microsoft.Data.OData.ODataException
at System.Web.Http.OData.Routing.DefaultODataPathHandler.ParseAtEntityCollection(IEdmModel model, ODataPathSegment previous, IEdmType previousEdmType, String segment) at System.Web.Http.OData.Routing.DefaultODataPathHandler.ParseAtCollection(IEdmModel model, ODataPathSegment previous, IEdmType previousEdmType, String segment) at System.Web.Http.OData.Routing.DefaultODataPathHandler.ParseNextSegment(IEdmModel model, ODataPathSegment previous, IEdmType previousEdmType, String segment) at System.Web.Http.OData.Routing.DefaultODataPathHandler.Parse(IEdmModel model, String odataPath) at System.Web.Http.OData.Routing.ODataPathRouteConstraint.Match(HttpRequestMessage request, IHttpRoute route, String parameterName, IDictionary`2 values, HttpRouteDirection routeDirection)
My code:
public class TasksPlanController : ODataController
{
private static ODataValidationSettings _validationSettings = new ODataValidationSettings();
// GET: odata/TasksPlan
[EnableQuery]
public List<Models.TasksList> Get(string module)
{
//var query= "";
using (var context = new TasksPlanEntities())
{
List<Models.TasksList> ts = new List<Models.TasksList>();
if (module.ToLower() == "PEMS".ToLower())
{
var query = from PIRTL in context.PIRTaskLists
select PIRTL;
ts = query.Select(x => new Models.TasksList { PIRCode = x.PIRCode, FunctionalLocation = x.FunctionalLocation, TaskName = x.TaskName, OperationCode = x.OperationCode, OperationNo = x.OperationNo, StartDate = x.StartDate, LastInspDate = x.LastInspDate, NextInspDate = x.NextInspDate }).ToList<Models.TasksList>();
}
return ts;
}
}
webapiconfig.cs
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
//config.MapHttpAttributeRoutes();
//config.Routes.MapHttpRoute(
// name: "DefaultApi",
// routeTemplate: "api/{controller}/{id}",
// defaults: new { id = RouteParameter.Optional }
//);
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<TasksList>("TasksPlan");
config.Routes.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
//var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
//config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
}
And also is that valid to call an odata based web api method?
You need to follow the default routing conventions unless you have customized them. In which case the Get method should likely return an IQueryable<TasksList> as queried directly from your DbSet.
I can see that you have some customized logic going on that determines if your return list is actually populated from the database in which case I suggest that you implement this logic as a custom OData Function (example here) where you can define custom parameters that define the logic of your query.
As an alternative approach that may work, you can try using your current code but change the parameter to [FromQuery] string module to indicate that the parameter isn't part of the OData route. It is also preferable to return an IQueryable<TasksList> directly instead of a new List object as the queryable is passed forward to the EnableQuery attribute filter.
Something like this:
// GET: odata/TasksPlan?module=PEMS
[EnableQuery]
public IQueryable<Models.TasksList> Get([FromQuery] string module)
{
using (var context = new TasksPlanEntities())
{
if (module.ToLower() == "PEMS".ToLower())
{
var query = from PIRTL in context.PIRTaskLists
select PIRTL;
return query.Select(x => new Models.TasksList { PIRCode = x.PIRCode, FunctionalLocation = x.FunctionalLocation, TaskName = x.TaskName, OperationCode = x.OperationCode, OperationNo = x.OperationNo, StartDate = x.StartDate, LastInspDate = x.LastInspDate, NextInspDate = x.NextInspDate });
}
return Enumerable.Empty<Models.TasksList>().AsQueryable();
}
}
I'm trying to implemented a custom HttpTaskAsyncHandler for my custom content management solution. The idea is to route /100/my-little-pony to /Details/my-little-pony. I hope to achieve this with the following HttpTaskAsyncHandler:
public override async Task ProcessRequestAsync(HttpContext context)
{
try
{
var id = GetContentIdFromRouteData();
// Retrieve the content identified by the specified ID
var contentRepository = new ContentRepository();
var content = await contentRepository.GetAsync(id);
if (content == null)
throw new ContentNotFoundException(id);
// Initialize an instance of the content controller
var factory = ControllerBuilder.Current.GetControllerFactory();
var controller = (IContentController) factory.CreateController(_requestContext, content.ControllerName);
if (controller == null)
throw new ControllerNotFoundException(content.ControllerName);
try
{
// Retrieve all content type values and pass them on the the method for index pages
var action = _requestContext.RouteData.GetRequiredString("action");
if (action == "Index")
{
ContentType data = null;
if (controller.ContentType != null)
{
data = BusinessHost.Resolve<ContentType>(controller.ContentType);
data.Values = content.Parameters.ToDictionary(p => p.Name, p => p.Value);
}
_requestContext.RouteData.Values.Add("data", data);
}
var values = _requestContext.RouteData.Values;
values.Add("name", content.Name);
values.Add("controllerId", id);
values.Add("controller", content.ControllerName);
controller.Execute(_requestContext);
}
finally
{
factory.ReleaseController(controller);
}
}
catch (ContentNotFoundException ex)
{
Trace.TraceWarning($"404: {ex.Message}");
_requestContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;
}
}
This works wonderfully well for synchronous requests, but when I try to invoke asynchronous methods ...
#using (Html.BeginForm("Save", Html.ControllerId(), FormMethod.Post, new { #class = "form-horizontal" }))
... and this being the method ...
[HttpPost]
public async Task<ActionResult> Save(NewsViewModel model)
{ }
Edit I've changed the name of the method to Save as Async isn't inferred, I receive a new error:
The asynchronous action method 'Login' returns a Task, which cannot be executed synchronously.
Action name is SaveAsync, but code that refers to it uses Save as the name. There is no magical renaming for any actions, including async once.
Your options:
use SaveAsync to refer to the action
use ActionName attribute to rename action
rename method to Save (but that would be against convention that all async methods have ...Async suffix)
Side note: using routing may be better option for redirects than some custom handler.
There's more to MvcHandler than meets the eye, and I've come to the realization that one cannot simply hope to replicate it with a clean conscience. I've therefore decided to change the way I approach this problem entirely: instead of trying to implement my own MvcHandler, I extend my IRouteHandler instead.
This is my solution:
private static ConcurrentDictionary<string, Type> _contentTypes = new ConcurrentDictionary<string, Type>();
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
// Retrieve the page ID from the route data
var id = GetContentIdFromRouteData(requestContext);
// Retrieve the content identified by the specified ID
var contentRepository = new ContentRepository();
var content = contentRepository.Get(id);
if (content == null)
throw new ContentNotFoundException(id);
// Retrieve all content type values and pass them on the the method for index pages
var action = requestContext.RouteData.GetRequiredString("action");
if (action == "Index")
{
var data = CreateContentType(requestContext, content);
requestContext.RouteData.Values.Add("data", data);
}
var values = requestContext.RouteData.Values;
values.Add("name", content.Name);
values.Add("controllerId", id);
values.Add("controller", content.ControllerName);
return new MvcHandler(requestContext);
}
private static int GetContentIdFromRouteData(RequestContext context)
{
var idString = context.RouteData.GetRequiredString("id");
int id;
if (!int.TryParse(idString, out id))
throw new ArgumentException("Content can't be loaded due to an invalid route parameter.", "id");
return id;
}
private static ContentType CreateContentType(RequestContext context, Content content)
{
Type type;
if (!_contentTypes.ContainsKey(content.ControllerName) ||
!_contentTypes.TryGetValue(content.ControllerName, out type))
{
var factory = ControllerBuilder.Current.GetControllerFactory();
var controller = (IContentController)factory.CreateController(context, content.ControllerName);
if (controller == null)
throw new ControllerNotFoundException(content.ControllerName);
type = controller.ContentType;
factory.ReleaseController(controller);
_contentTypes.TryAdd(content.ControllerName, type);
}
ContentType data = null;
if (type != null)
{
data = BusinessHost.Resolve<ContentType>(type);
data.Values = content.Parameters.ToDictionary(p => p.Name, p => p.Value);
}
return data;
}
I want to test if a URL is part of the routes defined in the Global.asax. This is what I have:
var TheRequest = HttpContext.Current.Request.Url.AbsolutePath.ToString();
var TheRoutes = System.Web.Routing.RouteTable.Routes;
foreach (var TheRoute in TheRoutes)
{
if (TheRequest == TheRoute.Url) //problem here
{
RequestIsInRoutes = true;
}
}
The problem is that I can’t extract the URL from the route. What do I need to change?
The problem is that I can't extract the URL from the route.
I disagree. The problem is that you expect to pull the URLs out of the route table and compare them externally. Furthermore, it is unclear what you hope to gain by doing so.
Routing compares the incoming request against business logic to determine if it matches. This is a route's purpose. Moving the matching logic outside of the route is not a valid test because you are not testing the business logic that is implemented by the route.
Not to mention, it is a bit presumptive to assume that a route can only match a URL and nothing else in the request such as form post values or cookies. While the built in routing functionality only matches URLs, there is nothing stopping you from making a constraint or custom route that matches other criteria.
So, in short you need to write unit tests for the business logic in your routes. Any logic that happens outside of your route configuration should be unit tested separately.
There is a great post by Brad Wilson (albeit a bit dated) that demonstrates how to unit test your routes. I have updated the code to work with MVC 5 - here is a working demo using the below code.
IncomingRouteTests.cs
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MvcRouteTesting;
using System.Web.Mvc;
using System.Web.Routing;
[TestClass]
public class IncomingRouteTests
{
[TestMethod]
public void RouteWithControllerNoActionNoId()
{
// Arrange
var context = new StubHttpContextForRouting(requestUrl: "~/controller1");
var routes = new RouteCollection();
RouteConfig.RegisterRoutes(routes);
// Act
RouteData routeData = routes.GetRouteData(context);
// Assert
Assert.IsNotNull(routeData);
Assert.AreEqual("controller1", routeData.Values["controller"]);
Assert.AreEqual("Index", routeData.Values["action"]);
Assert.AreEqual(UrlParameter.Optional, routeData.Values["id"]);
}
[TestMethod]
public void RouteWithControllerWithActionNoId()
{
// Arrange
var context = new StubHttpContextForRouting(requestUrl: "~/controller1/action2");
var routes = new RouteCollection();
RouteConfig.RegisterRoutes(routes);
// Act
RouteData routeData = routes.GetRouteData(context);
// Assert
Assert.IsNotNull(routeData);
Assert.AreEqual("controller1", routeData.Values["controller"]);
Assert.AreEqual("action2", routeData.Values["action"]);
Assert.AreEqual(UrlParameter.Optional, routeData.Values["id"]);
}
[TestMethod]
public void RouteWithControllerWithActionWithId()
{
// Arrange
var context = new StubHttpContextForRouting(requestUrl: "~/controller1/action2/id3");
var routes = new RouteCollection();
RouteConfig.RegisterRoutes(routes);
// Act
RouteData routeData = routes.GetRouteData(context);
// Assert
Assert.IsNotNull(routeData);
Assert.AreEqual("controller1", routeData.Values["controller"]);
Assert.AreEqual("action2", routeData.Values["action"]);
Assert.AreEqual("id3", routeData.Values["id"]);
}
[TestMethod]
public void RouteWithTooManySegments()
{
// Arrange
var context = new StubHttpContextForRouting(requestUrl: "~/a/b/c/d");
var routes = new RouteCollection();
RouteConfig.RegisterRoutes(routes);
// Act
RouteData routeData = routes.GetRouteData(context);
// Assert
Assert.IsNull(routeData);
}
[TestMethod]
public void RouteForEmbeddedResource()
{
// Arrange
var context = new StubHttpContextForRouting(requestUrl: "~/foo.axd/bar/baz/biff");
var routes = new RouteCollection();
RouteConfig.RegisterRoutes(routes);
// Act
RouteData routeData = routes.GetRouteData(context);
// Assert
Assert.IsNotNull(routeData);
Assert.IsInstanceOfType(routeData.RouteHandler, typeof(StopRoutingHandler));
}
}
OutgoingRouteTests.cs
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MvcRouteTesting;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
[TestClass]
public class OutgoingRouteTests
{
[TestMethod]
public void ActionWithAmbientControllerSpecificAction()
{
UrlHelper helper = GetUrlHelper();
string url = helper.Action("action");
Assert.AreEqual("/defaultcontroller/action", url);
}
[TestMethod]
public void ActionWithSpecificControllerAndAction()
{
UrlHelper helper = GetUrlHelper();
string url = helper.Action("action", "controller");
Assert.AreEqual("/controller/action", url);
}
[TestMethod]
public void ActionWithSpecificControllerActionAndId()
{
UrlHelper helper = GetUrlHelper();
string url = helper.Action("action", "controller", new { id = 42 });
Assert.AreEqual("/controller/action/42", url);
}
[TestMethod]
public void RouteUrlWithAmbientValues()
{
UrlHelper helper = GetUrlHelper();
string url = helper.RouteUrl(new { });
Assert.AreEqual("/defaultcontroller/defaultaction", url);
}
[TestMethod]
public void RouteUrlWithAmbientValuesInSubApplication()
{
UrlHelper helper = GetUrlHelper(appPath: "/subapp");
string url = helper.RouteUrl(new { });
Assert.AreEqual("/subapp/defaultcontroller/defaultaction", url);
}
[TestMethod]
public void RouteUrlWithNewValuesOverridesAmbientValues()
{
UrlHelper helper = GetUrlHelper();
string url = helper.RouteUrl(new
{
controller = "controller",
action = "action"
});
Assert.AreEqual("/controller/action", url);
}
static UrlHelper GetUrlHelper(string appPath = "/", RouteCollection routes = null)
{
if (routes == null)
{
routes = new RouteCollection();
RouteConfig.RegisterRoutes(routes);
}
HttpContextBase httpContext = new StubHttpContextForRouting(appPath);
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "defaultcontroller");
routeData.Values.Add("action", "defaultaction");
RequestContext requestContext = new RequestContext(httpContext, routeData);
UrlHelper helper = new UrlHelper(requestContext, routes);
return helper;
}
}
Stubs.cs
using System;
using System.Collections.Specialized;
using System.Web;
public class StubHttpContextForRouting : HttpContextBase
{
StubHttpRequestForRouting _request;
StubHttpResponseForRouting _response;
public StubHttpContextForRouting(string appPath = "/", string requestUrl = "~/")
{
_request = new StubHttpRequestForRouting(appPath, requestUrl);
_response = new StubHttpResponseForRouting();
}
public override HttpRequestBase Request
{
get { return _request; }
}
public override HttpResponseBase Response
{
get { return _response; }
}
public override object GetService(Type serviceType)
{
return null;
}
}
public class StubHttpRequestForRouting : HttpRequestBase
{
string _appPath;
string _requestUrl;
public StubHttpRequestForRouting(string appPath, string requestUrl)
{
_appPath = appPath;
_requestUrl = requestUrl;
}
public override string ApplicationPath
{
get { return _appPath; }
}
public override string AppRelativeCurrentExecutionFilePath
{
get { return _requestUrl; }
}
public override string PathInfo
{
get { return ""; }
}
public override NameValueCollection ServerVariables
{
get { return new NameValueCollection(); }
}
}
public class StubHttpResponseForRouting : HttpResponseBase
{
public override string ApplyAppPathModifier(string virtualPath)
{
return virtualPath;
}
}
With that out of the way, back to your original question.
How to determine if the URL is in the route table?
The question is a bit presumptive. As others have pointed out, the route table does not contain URLs, it contains business logic. A more correct way to phrase the question would be:
How to determine if an incoming URL matches any route in the route table?
Then you are on your way.
To do so, you need to execute the GetRouteData business logic in the route collection. This will execute the GetRouteData method on each route until the first one of them returns a RouteData object instead of null. If none of them return a RouteData object (that is, all of the routes return null), it indicates that none of the routes match the request.
In other words, a null result from GetRouteData indicates that none of the routes matched the request. A RouteData object indicates that one of the routes matched and it provides the necessary route data (controller, action, etc) to make MVC match an action method.
So, to simply check whether a URL matches a route, you just need to determine whether the result of the operation is null.
[TestMethod]
public void EnsureHomeAboutMatches()
{
// Arrange
var context = new StubHttpContextForRouting(requestUrl: "~/home/about");
var routes = new RouteCollection();
RouteConfig.RegisterRoutes(routes);
// Act
RouteData routeData = routes.GetRouteData(context);
// Assert
Assert.IsNotNull(routeData);
}
Note also that generating routes is a separate task from matching incoming routes. You can generate outgoing URLs from routes, but it uses a completely different set of business logic than matching incoming routes. This outgoing URL logic can (and should) be unit tested separately from the incoming URL logic as demonstrated above.
I don't know if this is what you want is the requested route, if that is the case you can get it from the current request:
var route = HttpContext.Current.Request.RequestContext.RouteData.Route;
You could try checking the current context against route table
var contextBase = HttpContext.Current.Request.RequestContext.HttpContext;
var data = RouteTable.Routes.GetRouteData(contextBase);
if (data != null) {
//Route exists
}
Using the above as a basis of creating a service
public interface IRouteInspector {
bool RequestIsInRoutes();
}
public interface IHttpContextAccessor {
HttpContextBase HttpContext { get; }
}
public interface IRouteTable {
RouteCollection Routes { get; }
}
public class RouteInspector : IRouteInspector {
private IRouteTable routeTable;
private IHttpContextAccessor contextBase;
public RouteInspector(IRouteTable routeTable, IHttpContextAccessor contextBase) {
this.routeTable = routeTable;
this.contextBase = contextBase;
}
public bool RequestIsInRoutes() {
if (routeTable.Routes.GetRouteData(contextBase.HttpContext) != null) {
//Route exists
return true;
}
return false;
}
}
And here is test class showing how it is used.
[TestClass]
public class RouteTableUnitTests : ControllerUnitTests {
[TestMethod]
public void Should_Get_Request_From_Route_Table() {
//Arrange
var contextBase = new Mock<IHttpContextAccessor>();
contextBase.Setup(m => m.HttpContext)
.Returns(HttpContext.Current.Request.RequestContext.HttpContext);
var routeTable = new Mock<IRouteTable>();
routeTable.Setup(m => m.Routes).Returns(RouteTable.Routes);
var sut = new RouteInspector(routeTable.Object, contextBase.Object);
//Act
var actual = sut.RequestIsInRoutes();
//Assert
Assert.IsTrue(actual);
}
}
There is room for refactoring and improvements but it's a start.
This is what I ended up doing:
string TheRequest = HttpContext.Current.Request.Url.AbsolutePath.ToString();
foreach (Route r in System.Web.Routing.RouteTable.Routes)
{
if (("/" + r.Url) == TheRequest)
{
//the request is in the routes
}
}
It's hacky but it works in 3 lines.
I'm using ASP.NET Web API 2 with attribute routing but i can't seem to get the versioning using media types application/vnd.company[.version].param[+json] to work.
I get the following error:
The given key was not present in the dictionary.
which originates from testing the key _actionParameterNames[descriptor] in FindActionMatchRequiredRouteAndQueryParameters() method.
foreach (var candidate in candidatesFound)
{
HttpActionDescriptor descriptor = candidate.ActionDescriptor;
if (IsSubset(_actionParameterNames[descriptor], candidate.CombinedParameterNames))
{
matches.Add(candidate);
}
}
Source: ApiControllerActionSelector.cs
After further debugging I've realized that if you have two controllers
[RoutePrefix("api/people")]
public class PeopleController : BaseApiController
{
[Route("")]
public HttpResponseMessage GetPeople()
{
}
[Route("identifier/{id}")]
public HttpResponseMessage GetPersonById()
{
}
}
[RoutePrefix("api/people")]
public class PeopleV2Controller : BaseApiController
{
[Route("")]
public HttpResponseMessage GetPeople()
{
}
[Route("identifier/{id}")]
public HttpResponseMessage GetPersonById()
{
}
}
you can't use your custom ApiVersioningSelector : DefaultHttpControllerSelector because it will test the keys,as stated above, from all controllers having the same [RoutePrefix("api/people")] and obviously an exception will be thrown.
Just to be sure the right controller was selected
I don't know if this is a bug, but using route [RoutePrefix("api/v1/people")] to version API makes me sad.
NOTE: This works great without attribute routing.
UPDATE
public class ApiVersioningSelector : DefaultHttpControllerSelector
{
private HttpConfiguration _HttpConfiguration;
public ApiVersioningSelector(HttpConfiguration httpConfiguration)
: base(httpConfiguration)
{
_HttpConfiguration = httpConfiguration;
}
public override HttpControllerDescriptor SelectController(HttpRequestMessage request)
{
IDictionary<string, HttpControllerDescriptor> controllers = GetControllerMapping();
var attributedRoutesData = request.GetRouteData().GetSubRoutes();
var subRouteData = attributedRoutesData.LastOrDefault(); //LastOrDefault() will get PeopleController, FirstOrDefault will get People{version}Controller which we don't want
var actions = (ReflectedHttpActionDescriptor[])subRouteData.Route.DataTokens["actions"];
var controllerName = actions[0].ControllerDescriptor.ControllerName;
//For controller name without attribute routing
//var controllerName = (string)routeData.Values["controller"];
HttpControllerDescriptor oldControllerDescriptor;
if (controllers.TryGetValue(controllerName, out oldControllerDescriptor))
{
var apiVersion = GetVersionFromMediaType(request);
var newControllerName = String.Concat(controllerName, "V", apiVersion);
HttpControllerDescriptor newControllerDescriptor;
if (controllers.TryGetValue(newControllerName, out newControllerDescriptor))
{
return newControllerDescriptor;
}
return oldControllerDescriptor;
}
return null;
}
private string GetVersionFromMediaType(HttpRequestMessage request)
{
var acceptHeader = request.Headers.Accept;
var regularExpression = new Regex(#"application\/vnd\.mycompany\.([a-z]+)\.v([0-9]+)\+json",
RegexOptions.IgnoreCase);
foreach (var mime in acceptHeader)
{
var match = regularExpression.Match(mime.MediaType);
if (match != null)
{
return match.Groups[2].Value;
}
}
return "1";
}
}
Thanks for the sharing your code. I have modified your version controller selector like below and tried some scenarios and it seems to work well. Can you try updating your controller selector like below and see if it works?
public override HttpControllerDescriptor SelectController(HttpRequestMessage request)
{
HttpControllerDescriptor controllerDescriptor = null;
// get list of all controllers provided by the default selector
IDictionary<string, HttpControllerDescriptor> controllers = GetControllerMapping();
IHttpRouteData routeData = request.GetRouteData();
if (routeData == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
//check if this route is actually an attribute route
IEnumerable<IHttpRouteData> attributeSubRoutes = routeData.GetSubRoutes();
var apiVersion = GetVersionFromMediaType(request);
if (attributeSubRoutes == null)
{
string controllerName = GetRouteVariable<string>(routeData, "controller");
if (controllerName == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
string newControllerName = String.Concat(controllerName, "V", apiVersion);
if (controllers.TryGetValue(newControllerName, out controllerDescriptor))
{
return controllerDescriptor;
}
else
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
}
else
{
// we want to find all controller descriptors whose controller type names end with
// the following suffix(ex: CustomersV1)
string newControllerNameSuffix = String.Concat("V", apiVersion);
IEnumerable<IHttpRouteData> filteredSubRoutes = attributeSubRoutes.Where(attrRouteData =>
{
HttpControllerDescriptor currentDescriptor = GetControllerDescriptor(attrRouteData);
bool match = currentDescriptor.ControllerName.EndsWith(newControllerNameSuffix);
if (match && (controllerDescriptor == null))
{
controllerDescriptor = currentDescriptor;
}
return match;
});
routeData.Values["MS_SubRoutes"] = filteredSubRoutes.ToArray();
}
return controllerDescriptor;
}
private HttpControllerDescriptor GetControllerDescriptor(IHttpRouteData routeData)
{
return ((HttpActionDescriptor[])routeData.Route.DataTokens["actions"]).First().ControllerDescriptor;
}
// Get a value from the route data, if present.
private static T GetRouteVariable<T>(IHttpRouteData routeData, string name)
{
object result = null;
if (routeData.Values.TryGetValue(name, out result))
{
return (T)result;
}
return default(T);
}