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); } }
作者:银龙
-------------------------------------------
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!