Copied to clipboard

Flag this post as spam?

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


  • Ado 8 posts 77 karma points
    Dec 17, 2015 @ 17:40
    Ado
    0

    How can I route this

    EDIT: I have managed to make it work, but is there a way to use actions with the same name but different parameters ? I only get ambiguos error when I try.

    How can i make this work in umbraco

    public class BloggController : RenderMvcController
     { 
    
        [Route("/{page:int}")]
        public ActionResult Blog(RenderModel model, int page)
                {
                  //Do stuff with page..
    
                 Return CurrentTemplate(model);
                }
    
        public ActionResult Blog(RenderModel model)
                {
                  //Do other stuff without page
    
                 Return CurrentTemplate(model);
                }
    

    I have this route in my startup.cs

     RouteTable.Routes.MapRoute(
                    "test",
                    "Blogg/{id}",
                    new
                    {
                        controller = "Blogg",
                        action = "Blogg",
                        id = UrlParameter.Optional
                    });
    

    But the rendermodel is null when I use this, and I need it in my action. I know there is a way of using the PluginController, and creating a CreateRenderModel function, and some other stuff. But that seems like a lot of hassle and I need to change code in a lot of places.

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Dec 17, 2015 @ 19:25
    Nicholas Westby
    0

    You can implement a custom ActionMethodSelectorAttribute that will allow you to apply special logic to choose between action methods during the routing process. I show how to do that here: http://www.codeproject.com/Articles/584908/Umbrazure-Limitless-Websites-with-Umbraco-on-Azure

    That article is obtusely long, so here's the appropriate code snippet:

    namespace Umbrazure.Web.Action_Selectors
    {
        using System;
        using System.Web.Mvc;
        public class AcceptParameterAttribute : ActionMethodSelectorAttribute
        {
            public string Name { get; set; }
            public string Value { get; set; }
            public override bool IsValidForRequest(
                ControllerContext controllerContext,
                System.Reflection.MethodInfo methodInfo)
            {
                var req = controllerContext.RequestContext.HttpContext.Request;
                bool valid;
                if (string.IsNullOrEmpty(this.Value))
                {
                    valid = !string.IsNullOrEmpty(req.Form[this.Name]);
                }
                else
                {
                    valid = string.Equals(req.Form[this.Name], this.Value,
                        StringComparison.InvariantCultureIgnoreCase);
                }
                return valid;
            }
        }
    }
    

    And here's an example of how to use it:

    namespace Umbrazure.Web.Controllers
    {
        using System.Web.Mvc;
        using Umbrazure.Web.Action_Selectors;
        public class HomeController : Umbraco.Web.Mvc.RenderMvcController
        {
    
            [HttpGet]
            public ActionResult MvcHome()
            {
                return CurrentTemplate(new Models.Home {
                    Text = "This text came from the default action method." });
            }
    
            [HttpPost]
            [ActionName("MvcHome")]
            [AcceptParameter(Name = "btnUpdate1")]
            public ActionResult MvcHome_1(Models.Home model)
            {
                ModelState.Clear();
                model.Text = "This text came from the first action method.";
                return CurrentTemplate(model);
            }
    
            [HttpPost]
            [ActionName("MvcHome")]
            [AcceptParameter(Name = "btnUpdate2")]
            public ActionResult MvcHome_2(Models.Home model)
            {
                ModelState.Clear();
                model.Text = "This text came from the second action method.";
                return CurrentTemplate(model);
            }
    
        }
    }
    

    In my case, I'm looking at the name of the button that submits the form to ascertain which action method to route to. You could use your own logic (e.g., yo can check if "ID" exists as a parameter in the request).

Please Sign in or register to post replies

Write your reply to:

Draft