Copied to clipboard

Flag this post as spam?

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


  • Reda 1 post 71 karma points
    Dec 05, 2015 @ 11:57
    Reda
    0

    Need to call an action in custom controller - RenderMvcController

    I'm new to Umbraco. I wasn't sure if I should use a surface controller or a custom controller for this scenario. I decided on custom controller mainly because I need the action to get called even if someone copies the action link to the browser.

    I want to change the language in a cookie:

    public class HeaderController : RenderMvcController
    {
        const string cookieName = "langCookie";
    
        public override ActionResult Index(RenderModel model)
        {
            //Do some stuff here, then return the base method
            return base.Index(model);
        }
    
    
        [HttpPost]
        public ActionResult LanguageCookieSet(RenderModel pModel, string pLang)
        {
            var langCookie = new HttpCookie(cookieName);
            var now = DateTime.Now;
    
            if (!string.IsNullOrWhiteSpace(pLang))
            {
                langCookie.Value = pLang;
                langCookie.Expires = now.AddDays(60);
    
                // Add the cookie.
                Response.Cookies.Add(langCookie);
            }
    
            return base.Index(pModel);
        }
    
        [HttpGet]
        public string LanguageCookieGet()
        {
            var langCookie = new HttpCookie(cookieName);
            langCookie = Request.Cookies[cookieName];
    
            return langCookie.ToString() ?? string.Empty; 
        }
    }
    

    On the view I tried calling it using: Url.Action like so: (didn't work) @Html.Action("LanguageCookieSet", "Header", new { pModel = this, pLanguage = "en" })"

    Then I tried: (also doesn't work)

    Error: No route in the route table matches the supplied values.

    Any suggestions will be very much appreciated.

  • Damiaan 442 posts 1301 karma points MVP 6x c-trib
    Dec 21, 2015 @ 17:05
    Damiaan
    0

    Hi,

    This is a tricky thing. The Methods of the RenderMvcController MUST match a template. In this case you NEED to have templates called LanguageCookieSet and LanguageCookieGet. This is not what you were planning I guess.

    It's probably easier to override a PrepareContentRequest method. Or create a contentFinder.

Please Sign in or register to post replies

Write your reply to:

Draft