tags:

views:

400

answers:

5

I'm getting myself acquainted with ASP.Net-MVC, and I was trying to accomplish some common tasks I've accomplished in the past with webforms and other functionality. On of the most common tasks I need to do is create SEO-friendly urls, which in the past has meant doing some url rewriting to build the querystring into the directory path.

for example: www.somesite.com/productid/1234/widget

rather than: www.somesite.com?productid=1234&name=widget

What method do I use to accomplish this in ASP.Net-MVC?

I've search around, and all I've found is this, which either I'm not understanding properly, or doesn't really answer my question:

http://stackoverflow.com/questions/734583/seo-urls-with-asp-net-mvc

A: 

Look at http://urlrewriter.net/ or http://www.urlrewriting.net/149/en/home.html

choudeshell
asp.net MVC ships with a complete url rewriting and routing solution out of the box
iAn
+6  A: 

MVC stands for "Model View Controller" and while those concepts aren't what you're asking about, you generally can wire up URL's like you see above quite easily

So for example by default the URL's look like the following

http://www.somesite.com/controller/view/

where controller refers to the controller class within your project, and view refers to the page/method combination within the controller. So for example you could write the view to take in an input and look something like the following

http://www.somesite.com/widget/productid/1234/

Now as for SEO Friendly URL's, that's just useless sugar. You author your controller such that it adds harmless cruft to the end of the URL.

So for example, you'll notice that the following three ways to get to this question produce the same result:

http://stackoverflow.com/questions/1325698/how-do-i-create-seo-friendly-urls-in-asp-net-mvc

http://stackoverflow.com/questions/1325698/

http://stackoverflow.com/questions/1325698/who-cares

Stack Overflow has authored their route values such that the bit that occurs after the question ID isn't really necessary to have.

So why have it there? To increase Google PageRank. Google PageRank relies on many things, the sum total of which are secret, but one of the things people have noticed is that, all other things being equal, descriptive text URL's rank higher. So that's why Stack Overflow uses that text after the question number.

Schnapple
+2  A: 

I think what you are after is MVC Routing have a look at these

Nathan Fisher
+4  A: 

Create a new route in the Global.asax to handle this:

        routes.MapRoute(
            "productId",                  // Route name
            "productId/{id}/{name}",      // URL with parameters
            new { controller = "Home", action = "productId", id = 1234, name = widget }  // Parameter defaults
        );

Asp.Net MVC has routing built in, so no need for the Url Rewriter.

Martin
+1  A: 

It is also important to handle legacy urls. I have a database of old and new urls and I redirect with the following code;

    public class AspxCatchHandler : IHttpHandler, IRequiresSessionState
{

    #region IHttpHandler Members

    public bool IsReusable
    {
        get { return true; }
    }

    public void ProcessRequest(HttpContext context)
    {
        if (context.Request.Url.AbsolutePath.Contains("aspx") && !context.Request.Url.AbsolutePath.ToLower().Contains("default.aspx"))
        {
            string strurl = context.Request.Url.PathAndQuery.ToString();
            string chrAction = "";
            string chrDest = "";

            try
            {

                DataTable dtRedirect = SqlFactory.Execute(
                    ConfigurationManager.ConnectionStrings["emptum"].ConnectionString,
                    "spGetRedirectAction",
                    new SqlParameter[] { 
                        new SqlParameter("@chrURL", strurl)
                    },
                    true);

                chrAction = dtRedirect.Rows[0]["chrAction"].ToString();
                chrDest = dtRedirect.Rows[0]["chrDest"].ToString();

                chrDest = context.Request.Url.Host.ToString() + "/" + chrDest;
                chrDest = "http://" + chrDest;


                if (string.IsNullOrEmpty(strurl))
                    context.Response.Redirect("~/");
            }
            catch
            {
                chrDest = "/";// context.Request.Url.Host.ToString();
            }

            context.Response.Clear();
            context.Response.Status = "301 Moved Permanently";
            context.Response.AddHeader("Location", chrDest);
            context.Response.End();

        }
        else
        {
            string originalPath = context.Request.Path;
            HttpContext.Current.RewritePath("/", false);
            IHttpHandler httpHandler = new MvcHttpHandler();
            httpHandler.ProcessRequest(HttpContext.Current);
            HttpContext.Current.RewritePath(originalPath, false);
        }
    }

    #endregion
}

hope this is useful

Desiny