C# 自定义路由配置

1、Gloabal.asax.cs
using System.Web.Mvc;
using System.Web.Routing;

namespace B1EC2.CqhgCallback
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            GlobalFilters.Filters.Add(new HandleErrorAttribute());

            var routes = RouteTable.Routes;
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            //routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "CqhgCallback", action = "Index", id = UrlParameter.Optional });
            routes.MapRoute("Default", "{tenatId}/{controller}/{action}/{id}", new { tenatId = "1", controller = "CqhgCallback", action = "Index", id = UrlParameter.Optional });

        }
    }
}


2、CqhgCallbackController.cs

using System;
using System.Web;
using System.Web.Mvc;
using B1EC2.CqhgCallback.Infrastructure;
using B1EC2.CqhgCallback.Models;
using Common.Logging;

namespace B1EC2.CqhgCallback.Controllers
{
    #region Post
    public class Post
    {
        [AllowHtml]
        public String data { get; set; }
        public Post() { }
    }
    #endregion

    public class CqhgCallbackController : Controller
    {
        readonly DefaultDbContext _dbContext;
        protected static readonly ILog Log = LogManager.GetLogger(typeof(CqhgCallbackController));

        public CqhgCallbackController()
        {
            _dbContext = new DefaultDbContext();
        }

        public String Index()
        {
            return "请使用Http Post调用!";
        }

        [HttpPost]
        [AllowAnonymous]
        public String Index(Post post,string tenatId)
        {
            string tenantIdStr = RouteData.Values["tenatId"].ToString();
            post.data = XmlHelper.DecodeBase64(post.data);
            post.data = SubPostData(post.data);

            var validate = Validate(post);
            if (!string.IsNullOrEmpty(validate))
                return validate;
            try
            {
               
        }
        private string SubPostData(string data) {
            data = data.Substring(data.IndexOf(""));
            data = data.Substring(0,data.IndexOf("")+14);

            return data;
        }

        protected override void Dispose(bool disposing)
        {
            if (_dbContext != null)
            {
                _dbContext.Dispose();
            }
            base.Dispose(disposing);
        }

        #region 验证一些问题

        public string Validate(Post pos)
        {
            var validate = string.Empty;
            if(string.IsNullOrEmpty(pos.data))
                validate += "XML数据不能为空!";

            return validate;
        }
        #endregion
    }
}


3、关键配置

 routes.MapRoute("Default", "{tenatId}/{controller}/{action}/{id}", new { tenatId = "1", controller = "CqhgCallback", action = "Index", id = Ur

虽然这里tennatId="1",但当你请求时此处的tenantId会被你请求的覆盖掉,也就是你请求是的参数,这样在下面controller中获取时就会根据不同请求参数得到不同的tenantId,这样就可以将一个回传地址或网站来提供给不同用户使用,仅是tenantId不同

postman请求



代码debug



post请求成功


你可能感兴趣的:(C#)