Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Getting a HARD URL for Your own Action

Getting a HARD URL for Your own Action

Scheduled Pinned Locked Moved ASP.NET
databasetutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TheOnlyRealTodd
    wrote on last edited by
    #1

    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.

    F 1 Reply Last reply
    0
    • T TheOnlyRealTodd

      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.

      F Offline
      F Offline
      F ES Sitecore
      wrote on last edited by
      #2

      Create your helper like this

      UrlHelper helper = new UrlHelper(HttpContext.Current.Request.RequestContext);

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups