欢迎来到银龙的博客

人生三从境界:昨夜西风凋碧树,独上高楼,望尽天涯路。 衣带渐宽终不悔,为伊消得人憔悴。 众里寻他千百度,蓦然回首,那人却在灯火阑珊处。

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);
        }

    }

 

posted on 2019-12-13 17:17  银龙科技  阅读(771)  评论(0编辑  收藏  举报

导航