Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Name- and type-safe ASP.NET MVC url creation
using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Web.Mvc;
using System.Web.Routing;
namespace MonkeyBusters.Web.Mvc
{
public static class SafeMvcUrls
{
public static RedirectToRouteResult RedirectToActionPermanent<C>(this Controller c, Expression<Func<C, ActionResult>> call)
where C : IController
{
return c.RedirectToAction(call, true);
}
public static RedirectToRouteResult RedirectToAction<C>(this Controller c, Expression<Func<C, ActionResult>> call, Boolean permenant = false)
where C : IController
{
var mvcAction = Get<C>(call);
mvcAction.Parameter.Add("controller", GetControllerName(typeof(C)));
mvcAction.Parameter.Add("action", mvcAction.Action.Name);
return new RedirectToRouteResult(mvcAction.Parameter);
}
public static String Action<C>(this UrlHelper url, Expression<Func<C, ActionResult>> call)
where C : IController
{
var mvcAction = Get<C>(call);
return url.Action(mvcAction.Action.Name, GetControllerName(mvcAction.Controller), mvcAction.Parameter);
}
public static String Action<C>(this UrlHelper url, Expression<Func<C, ActionResult>> call, String protocol)
where C : IController
{
var mvcAction = Get<C>(call);
return url.Action(mvcAction.Action.Name, GetControllerName(mvcAction.Controller), mvcAction.Parameter, protocol);
}
public static String Action<C>(this UrlHelper url, Expression<Func<C, ActionResult>> call, String protocol, String hostname)
where C : IController
{
var mvcAction = Get<C>(call);
return url.Action(mvcAction.Action.Name, GetControllerName(mvcAction.Controller), mvcAction.Parameter, protocol, hostname);
}
static String GetControllerName(Type controllerType)
{
var typeName = controllerType.Name;
if (typeName.ToLower().EndsWith("controller"))
{
return typeName.Substring(0, typeName.Length - "controller".Length);
}
else
{
return typeName;
}
}
class MvcAction
{
public Type Controller { get; set; }
public MethodInfo Action { get; set; }
public RouteValueDictionary Parameter { get; set; }
}
static MvcAction Get<C>(Expression<Func<C, ActionResult>> x)
where C : IController
{
var root = x.Body as MethodCallExpression;
if (root == null) throw new ArgumentException("Call expression expected.");
var method = root.Method;
var parameters = method.GetParameters();
var arguments = root.Arguments;
var routeValues = new RouteValueDictionary();
for (var i = 0; i < parameters.Length; ++i)
{
try
{
routeValues[parameters[i].Name] = Evaluate(arguments[i]);
}
catch (Exception ex)
{
throw new Exception(String.Format("Failed to evaluate argument #{0} of an mvc action call while creating a url, look at the inner exceptions.", i), ex);
}
}
return new MvcAction()
{
Controller = typeof(C),
Action = root.Method,
Parameter = routeValues
};
}
static Object Evaluate(Expression e)
{
if (e is ConstantExpression)
{
return (e as ConstantExpression).Value;
}
else
{
return Expression.Lambda(e).Compile().DynamicInvoke();
}
}
}
}
@Mart-Bogdan

This comment has been minimized.

Copy link

@Mart-Bogdan Mart-Bogdan commented Aug 4, 2015

I think we should make Library and post it on Nuget. I can make assistant in this.

@jtheisen

This comment has been minimized.

Copy link
Owner Author

@jtheisen jtheisen commented Apr 19, 2016

@Mart-Bogdan Just saw your comment almost a year later...

It is a library now, and a nuget package. See my safe-mvc-urls project on GitHub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.