Getting a HARD URL for Your own Action
-
Does anyone know how to create a simple hard URL to an action in MVC5? I'm not using it in the context of a View though, I am generating a unique URL and storing it into the database... Trying to use this:
public IHttpActionResult Create(Url url)
{
UrlHelper helper = new UrlHelper();
if (!ModelState.IsValid)
{
BadRequest();
}
url.OurUrl = helper.Action("Go","Redirect", null, "http") + url.UrlId;
_context.Urls.Add(url);
_context.SaveChanges();return Created(new Uri(Request.RequestUri + "/" + url.UrlId), Url);
}I've tried all kinds of variations on the Action() method... Like just using Go and Redirect as arguments... And what you see there. But it keeps telling me that routeCollection cannot be null I'm confused because routeCollection isn't even a parameter listed in the method. Thanks.
-
Does anyone know how to create a simple hard URL to an action in MVC5? I'm not using it in the context of a View though, I am generating a unique URL and storing it into the database... Trying to use this:
public IHttpActionResult Create(Url url)
{
UrlHelper helper = new UrlHelper();
if (!ModelState.IsValid)
{
BadRequest();
}
url.OurUrl = helper.Action("Go","Redirect", null, "http") + url.UrlId;
_context.Urls.Add(url);
_context.SaveChanges();return Created(new Uri(Request.RequestUri + "/" + url.UrlId), Url);
}I've tried all kinds of variations on the Action() method... Like just using Go and Redirect as arguments... And what you see there. But it keeps telling me that routeCollection cannot be null I'm confused because routeCollection isn't even a parameter listed in the method. Thanks.
Create your helper like this
UrlHelper helper = new UrlHelper(HttpContext.Current.Request.RequestContext);