MVC Routing

Last post 09-17-2009 11:18 PM by andrewjboyd. 3 replies.

Sort Posts:

  • MVC Routing

    09-17-2009, 8:26 PM
    • Participant
      804 point Participant
    • andrewjboyd
    • Member since 05-15-2009, 1:26 AM
    • Brisbane, Australia
    • Posts 173

    Hello everyone,

    I wish to have an MVC application that allows me to have a url like

    http://www.somedomain.com/andrew

    and then it would display details for andrew.

    So far, I have setup my Global.asax like so

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    
    namespace TestMVC
    {
        public class MvcApplication : System.Web.HttpApplication
        {
            public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
                routes.MapRoute(
                    "Root",
                    "{name}",
                    new { controller = "Account", action = "Index", name = "" }
                );
            }
    
            protected void Application_Start()
            {
                RegisterRoutes(RouteTable.Routes);
            }
        }
    }


    And my controller looks like this

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace TestMVC
    {
        [HandleError]
        public class AccountController : Controller
        {
            public ActionResult Index(string name)
            {
                ViewData["Name"] = name;
                return View();
            }
        }
    }
    


    And my view has

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Welcome</title>
    </head>
    <body>
        <div>
            Hello <%=ViewData["Name"] %>
        </div>
    </body>
    </html>


    At this stage, Name doesn't have a value (even though my url is http://localhost:1057/Andrew)

    This application only has 1 controller and 1 view, so I am just wondering how I can get something like this to work as expected.

    Dont forget to click "Mark as answer" on the post that helped you.

    ================================================
    Why catch a fish to feed someone, when you can teach them to fish and they can feed themselves
  • Re: MVC Routing

    09-17-2009, 10:54 PM
    • Contributor
      6,620 point Contributor
    • paul.vencill
    • Member since 02-01-2006, 12:57 PM
    • Gaithersburg, MD
    • Posts 1,350

     Nothing jumps out at me as a reason for it not to work based on what you posted here.  Can you set a breakpoint in your AccountController's Index action to make sure it's actually the action being called?

    Help those who have helped you... remember to "Mark as Answered"
  • Re: MVC Routing

    09-17-2009, 11:14 PM
    • Participant
      804 point Participant
    • andrewjboyd
    • Member since 05-15-2009, 1:26 AM
    • Brisbane, Australia
    • Posts 173

    It does get called, the break point gets picked up :)

    Dont forget to click "Mark as answer" on the post that helped you.

    ================================================
    Why catch a fish to feed someone, when you can teach them to fish and they can feed themselves
  • Re: MVC Routing

    09-17-2009, 11:18 PM
    Answer
    • Participant
      804 point Participant
    • andrewjboyd
    • Member since 05-15-2009, 1:26 AM
    • Brisbane, Australia
    • Posts 173

    I had setup the "Virtual Path" wrongly in the Project Properties, works a treat now :)

    Dont forget to click "Mark as answer" on the post that helped you.

    ================================================
    Why catch a fish to feed someone, when you can teach them to fish and they can feed themselves
Page 1 of 1 (4 items)