Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6855041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:42:28+00:00 2026-05-27T01:42:28+00:00

Ok, im developing a MVC CMS like website and when declaring routes i used

  • 0

Ok, im developing a MVC CMS like website and when declaring routes i used following pattern. I encapsulate action name and controller name into a class like so

public class UrlUtilsUnhandledErrorsExtensions
{
    private readonly UrlHelper _urlHelper;

    public UrlUtilsUnhandledErrorsExtensions(UrlHelper urlHelper)
    {
        _urlHelper = urlHelper;
    }

    public String GetLatestErrors()
    {
        return _urlHelper.Action("GetLatestErrors", "UnhandledErrors");
    }
}

Then instead of writing

@Url.Action("GetLatestErrors", "UnhandledErrors")

I write

@Url.Action(Url.Utils().UnhandledErrors().GetLatestErrors())

I find this approach much more easier to maintain, because if controller name changes i only have to change one class.

This works fine with any links, controller redirects (return Redirect(…)) and just anything that accept virtual path which is returned by

public String GetLatestErrors()
{
    return _urlHelper.Action("GetLatestErrors", "UnhandledErrors");
}

But here comes the problem: i cant use Html.Action() with this approach. It requires controller name and action name, but instead i want it to use virtual path.
After digging around and studying MVC source code i realized that i will need to write my own Html.Action extension method that will just accept virtual path.

So here is my solution

    public void ActionFromUrl(this HtmlHelper htmlHelper, String url)
    {
        RouteValueDictionary rvd = null;

        rvd = new RouteValueDictionary();

        String action = String.Empty;
        String controller = String.Empty;

        foreach (Route route in htmlHelper.RouteCollection)
        {
            if (route.Url == url.Substring(1)) // url starts with / for some reason
            {
                action = route.Defaults["action"] as String;
                controller = route.Defaults["controller"] as String;

                break;
            }
        }

        RequestContext rc = ((MvcHandler)HttpContext.Current.CurrentHandler).RequestContext;

        rc.RouteData.Values["action"] = action;

        rc.RouteData.Values["controller"] = controller;

        IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory();
        IController controllerImpl = factory.CreateController(rc, controller);
        controllerImpl.Execute(rc);
    }

It works, but since its based on Html.RenderAction method it just writes directly to output, so in my view when i write following code

@{ Html.ActionFromUrl(Url.Utils().UnhandledErrors().GetLatestErrors()); }

It renders my partial first, all above everything and then rest of html follows.
This is not the result i want, so i have to find out the way of rendering the result to string as Html.Action do. I already looked into the source code with dotPeek but coudn’t figure out how to mix it altogether.

My question is: Am i doing something wrong ? Or how can i write Html.Action overload so it accepts virtual path and returns MvcHtmlString ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:42:29+00:00Added an answer on May 27, 2026 at 1:42 am

    in CMS, you probably do not need the whole convention-based view rendering, you will (sooner or later) want render custom templates “to string” and merge result layout by your (most probably dynamic/configurable) rules. Take a look on RazorEngine project.

    http://razorengine.codeplex.com/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing a MVC .Net site and would like to implement a CMS system.
I'm developing (.NET MVC) a large website which has the following specifications: Database pages
I'm developing my first MVC website and I'm experimenting with the few available grids
I'm currently developing a CMS for a website I'm making. I'm using MVCScaffolding to
We are developing an MVC app using STS. We used the WIF tools to
I am developing an application using MVC Preview 5. I have used typed views.
I am developing a php MVC framework from scratch and have run into an
I'm developing an MVC 3 app I want to have a url something like:
I am developing an MVC 3 application in C# and I would like to
Hello guys i am developing MVC application, I want to call a action (with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.