1

i have this (simple) code

<% Html.RenderAction("Version", "Generic"); %>

in my masterpage of my asp.net mvc 2 app. This method returns the version of the application.

i also have this code in my controller:

class GenericController : BaseController
    {
        [ChildActionOnly]
        public string Version()
        {
            try
            {
                string assemblyFile = Assembly.GetCallingAssembly().FullName;
                FileInfo fi = new FileInfo(assemblyFile);
                string version = fi.LastWriteTime.Year.ToString( ) + fi.LastWriteTime.Month.ToString() + fi.LastWriteTime.Day.ToString();
                return version;
            }
            catch (Exception e)
            {
                return "1.0";

            }
        }
    }

Now i get this error: Execution of the child request failed. Please examine the InnerException for more information.

and the innerexcpetion is:

"The controller for path '/Account/LogOn' was not found or does not implement IController."

What i was thinking is that maybe the code can't execute because the user is not logged on yet, and tries to redirect to the logon method etc.

So the first thing i was thinking is to grant access in the web.config (like i do with the directory that has the css and images in it, it should also be accessable when you're not logged on:

<location path="Content">
        <system.web>
            <authorization>
                <allow users="*" />
            </authorization>
        </system.web>
    </location>

but what is the path for this (version) method ?

(or maybe there is another reason for the excpetion, i'm open for that answer too :))

ocuenca
  • 38,548
  • 11
  • 89
  • 102
Michel
  • 23,085
  • 46
  • 152
  • 242

1 Answers1

0

Normally controller actions return ActionResult and not strings. Also you should make sure that the BaseController you are deriving from doesn't have the [Authorize] attribute, in fact looking at your code you don't even need to derive from it. It could be simply:

public class GenericController
{
    [ChildActionOnly]
    public ActionResult Version()
    {
        try
        {
            string assemblyFile = Assembly.GetCallingAssembly().FullName;
            FileInfo fi = new FileInfo(assemblyFile);
            string version = fi.LastWriteTime.Year.ToString( ) + fi.LastWriteTime.Month.ToString() + fi.LastWriteTime.Day.ToString();
            return Content(version, "text/html");
        }
        catch (Exception e)
        {
            return Content("1.0", "text/html");
        }
    }
}

Also notice that I defined the controller a public class which was not your case. Finally remove the <location> section from your web.config. This is no longer used in ASP.NET MVC.

Also try rendering the action like this:

<%= Html.Action("Version", "Generic") %>

As a final remark I would probably remove the try/catch from the controller action. Why return a wrong version in case of exception?

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928