如何使用url路由添加新控制器? [英] How can I add new controller with url routing?

查看:73
本文介绍了如何使用url路由添加新控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,提供一些博客数据和图片。我为博客和图片创建了控制器和操作。



我更新了Global.asax.cs以创建用户友好的网址。没问题,直到这里。



现在我想添加一个简单的控制器,它是Comment和一个AddComment的动作。



当我做这个平常的操作,并试图访问MyProject / Comment / AddComment;它返回404找不到错误。



如何更新我的global.asax.cs以添加评论?

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?

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.

推荐答案

正如我从你的代码中看到的那样,你为每个动作创建了不同的路线。

可惜你已经更新了默认路线



现在如果你想添加一个新动作,你必须创建新路线为了那个原因。这样的事情:

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:
routes.MapRoute(
    "Comment", 
    "{controller}/{action}/{id}", 
    new { controller = "Comment", action = "AddComment", ad = UrlParameter.Optional });





-KR



-KR


这篇关于如何使用url路由添加新控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆