ASP.NET MVC5 与 API 自定义路由

 -----------------------------------------------------------------

MVC5

    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            //伪静态
            routes.MapMvcAttributeRoutes();

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }

HomeController

    [RoutePrefix("a/File")]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        [Route("About")]
        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }

 

------------------------------------------------------------------

API

WebApiConfig

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            //匹配注解路由
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }

FileController

    /// <summary>
    /// 普通文件上传接口,及文件查询接口
    /// </summary>
    [RoutePrefix("api/File")]
    public class FileController : ApiController
    {
        private readonly string _swfTools = WebConfigurationManager.AppSettings["SWFTools"].ToString();
        private readonly string _ffMpegTools = WebConfigurationManager.AppSettings["FFmpeg"].ToString();
        private readonly string _uploadDomain = WebConfigurationManager.AppSettings["UploadDomain"];

        /// <summary>
        /// 查询文件
        /// </summary>
        /// <param name="md5"></param>
        /// <returns></returns>
        [HttpGet]
       [Route("GetByMd5")]
        public string GetByMd5(string md5)
        {
            FilesService filesService = new FilesService();
            var dto = filesService.QuerySingle(md5, null);
            return JsonConvert.SerializeObject(dto);
        }

    }

 

ASP.NET MVC5 与 API 自定义路由

上一篇:Windows安装gmpy2


下一篇:Golang 在 Mac、Linux、Windows 下如何交叉编译