Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a project that serves some blog data and pictures. I created controllers and also actions for both blog and pictures.

I updated Global.asax.cs to create user friendly urls. No problem till here.

Now I want to add a simple controller which is Comment and an action which is AddComment.

When I did this usual operations, and tried to access MyProject/Comment/AddComment; it returns 404 not found error.

How should I update my global.asax.cs for adding comment?
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace PersonalApp
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{type}/{id}", // URL with parameters
                new { controller = "Blog", action = "Index", id = UrlParameter.Optional, type = UrlParameter.Optional } // Parameter defaults
            );

            routes.MapRoute(
              "BlogFix", // Route name
              "{controller}/{action}/{id}", // URL with parameters
              new { controller = "Blog", action = "Index", ad = UrlParameter.Optional } // Parameter defaults
          );



            routes.MapRoute(
                 "ArticleWithoutAction",              // Article Without Action name
                 "{controller}/{gelenId}/{title}/{date}",
                 new
                 {
                     controller = "Blog",
                     action = "ReadMore",
                     gelenId = UrlParameter.Optional,
                     title = UrlParameter.Optional,
                     date = UrlParameter.Optional
                 } // Parameter defaults
             );

            routes.MapRoute(
                "ArticleWithAction",                  // Article With Action name 
                "{controller}/{action}/{gelenId}/{title}/{date}",
                new
                {
                    controller = "Blog",
                    action = "ReadMore",
                    gelenId = UrlParameter.Optional,
                    title = UrlParameter.Optional,
                    date = UrlParameter.Optional
                } // Parameter defaults
            );

            routes.MapRoute(
                "DefaultB", // Route name
                "{controller}/{action}",
                new
                {
                    controller = "Blog",
                    action = "ReadMore"
                } // Parameter defaults
            );






            routes.MapRoute(
                "DefaultOfImage", // Route name
                "{controller}/{type}/{id}", // URL with parameters
                new { controller = "Image", action = "Index", id = UrlParameter.Optional, type = UrlParameter.Optional } // Parameter defaults
            );

            routes.MapRoute(
              "ImageFix", // Route name
              "{controller}/{action}/{id}", // URL with parameters
              new { controller = "Image", action = "Index", ad = UrlParameter.Optional } // Parameter defaults
          );

            routes.MapRoute(
                 "ImageWithoutAction",              // Image Without Action name
                 "{controller}/{gelenId}/{title}/{date}",
                 new
                 {
                     controller = "Image",
                     action = "Show",
                     gelenId = UrlParameter.Optional,
                     title = UrlParameter.Optional,
                     date = UrlParameter.Optional
                 } // Parameter defaults
             );

            routes.MapRoute(
                "ImageWithAction",                  // Image With Action name 
                "{controller}/{action}/{gelenId}/{title}/{date}",
                new
                {
                    controller = "Image",
                    action = "Show",
                    gelenId = UrlParameter.Optional,
                    title = UrlParameter.Optional,
                    date = UrlParameter.Optional
                } // Parameter defaults
            );

            routes.MapRoute(
                "DefaultImage2", // Route name
                "{controller}/{action}",
                new
                {
                    controller = "Image",
                    action = "Show"
                } // Parameter defaults
            );


          
       







        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }
}



Thanks.
Posted
Comments
[no name] 14-Oct-15 0:25am    
What is the complete URL you are browsing? Secondly what is "MyProject" in your URL.
FoxRoot 17-Oct-15 7:05am    
blabla.com/Blog/Category/AnythingElse
blabla.com/Image/AnythingElse

And I want to Add
blabla.com/Comment/AddComment

MyProject is the name of project. Nothing more.

1 solution

As I can see from you code, you have created different routes for your each action.
And pity that you've updated the default route too.

So now if you want to add one new action, you have to create the new route for that. Something like this:
C#
routes.MapRoute(
    "Comment", 
    "{controller}/{action}/{id}", 
    new { controller = "Comment", action = "AddComment", ad = UrlParameter.Optional });


-KR
 
Share this answer
 
Comments
FoxRoot 17-Oct-15 7:03am    
Thanks for your help. I tried that before asking. However this just blot out other url's. There is something wrong.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900