Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • David Houghton 33 posts 108 karma points
    Jan 02, 2018 @ 11:24
    David Houghton
    0

    Redirect to custom url (routing?)

    Hi guys, firstly happy new year!

    Having a little trouble with returning a custom view on a dynamic url, mainly through my own lack of understanding on how the process works so if someone can step in here and help me out id be extreemly grateful.

    How all this works is as follows:

    • a user selects the amount of tickets that are needed, on submit this hits a controller which builds up the model, ultimately hitting a RedirectToAction method:

        TempData["model"] = model;
        return RedirectToAction("ShowCompleteRegistrationForm");
    }
    
    public virtual ActionResult ShowCompleteRegistrationForm()
    {
         var model = (TicketsFormModel)TempData["model"];
         return View("~/Views/CompleteRegistration.cshtml", model);
    }
    

    Now for the most part this works, however the returned url is:

    /umbraco/Surface/{controllername}/{action}
    

    I know this is where all locally created surfact controllers get mapped to, however I need this to return the current url appending on /register to the end, so for example I need:

    /events/{year}/{eventName}/register
    

    Ive looked into route hijacking but I admit I dont fully understand how all this works, especially with the dynamic nature of the returned url.

  • Tom 34 posts 154 karma points
    Jan 02, 2018 @ 11:32
    Tom
    0

    You could add a new route to your routeConfig.cs file:

    routes.MapRoute(
        name: "Register",
        url: "events/{year}/{eventName}/register",
        defaults: new { controller = "Events", action = "Register" }
    );
    
  • David Houghton 33 posts 108 karma points
    Jan 02, 2018 @ 11:48
    David Houghton
    0

    Hi Tom, thanks for the reply, added a new RouteConfig.cs file into the root of App_start and this file contains:

    using System.Web.Mvc;
    using System.Web.Routing;
    
        public class RouteConfig
        {
    
            public static void RegisterRoutes(RouteCollection routes)
            {
                routes.MapRoute(
                    name: "Register",
                    url: "events/{year}/{eventName}/register",
                    defaults: new { controller = "{controllerName}", action = "Submit" }
                );
            }
        }
    

    However, still this redirects to

    /umbraco/Surface/{controller}/{action}
    

    I suspect theres more to getting this to work than just adding what you put above into a file. Im really not familiar with this, so whilst the help is appreciated I would understand more with an example. Honestly, speak to me like a 5 year old here, thanks for the help.

  • Tom 34 posts 154 karma points
    Jan 02, 2018 @ 13:37
    Tom
    0

    Hi David,

    No problem, i'll elaborate some more. When you've got the routeconfig file set-up you need to register it on the applicationstarted event.

    public class UmbracoApplication : ApplicationEventHandler {
        protected override void ApplicationStarted(UmbracoApplicationBase httpApplication, ApplicationContext applicationContext) {
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
    }
    

    For more information on the topic of binding events before, during or after start-up of your application check out: https://our.umbraco.org/documentation/reference/events/application-startup

    When creating a new controller which 'fetches' your request be sure to inherit from the base mvc Controller class.

    public class EventsController : Controller {
        public ActionResult Register(string year, string eventName) {
            return View();
        }
    }
    

    For the sake of completeness the route config file looks like:

    public class RouteConfig {
        public static void RegisterRoutes(RouteCollection routes) {
            routes.MapRoute(
                name: "Register",
                url: "events/{year}/{eventName}/register",
                defaults: new { controller = "Events", action = "Register", year = 2018, eventName = "test" }
            );
         }
    }
    

    (There's no need for the default year and eventName parameters to be set)

  • Tom 34 posts 154 karma points
    Jan 02, 2018 @ 13:42
    Tom
    0

    The documentation also has a topic + example on creating custom routes: https://our.umbraco.org/documentation/Reference/Routing/custom-routes

  • sai kiran 4 posts 93 karma points
    Feb 24, 2020 @ 04:43
    sai kiran
    0

    Similar kind of problem as above stated by David

    Hi Guys,

    Having trouble in returning to view by passing model along with this.

    errorMessage Cannot bind source type eCaptis.Models.Patient to model type Umbraco.Core.Models.PublishedContent.IPublishedContent.

    public class PatientController : SurfaceController {

    public ActionResult Edit(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Patient patient = db.Patients.Find(id);
        if (patient == null)
        {
            return HttpNotFound();
        }
        return PartialView( patient);
    }
    

    } I possibly suspect there is problem with the routing, as after installing Umbraco v8, Debugger is not hitting RouteConfig.cs

    Bit of BackGround: I have created a document type in umbraco called home which has a child item edit and login

    Home page will display a table where by clicking on name it should navigate to edit patient view, so i have passed Id of the patient to controller upon clicking the name of patient as shown in the code above.

    Doing this my url is changing from http://localhost:12345 to http://localhost:12345/umbraco/Surface/Patient/Edit/345

    In general if we dont use surface controller or Umbraco this url could be like http://localhost:12345 to http://localhost:12345/Patient/Edit/345

    I have even looked into route hijacking but this did not help me or may be because of my lack of understanding.

    Any help in fixing this is much appreciated Thanks in advance

Please Sign in or register to post replies

Write your reply to:

Draft