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("<DTC_Message>"));
            data = data.Substring(0,data.IndexOf("</DTC_Message>")+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、关键配置

<span style="font-family: Arial, Helvetica, sans-serif;"> routes.MapRoute("Default", "{tenatId}/{controller}/{action}/{id}", new { tenatId = "1", controller = "CqhgCallback", action = "Index", id = Ur</span>

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

postman请求



代码debug



post请求成功


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值