asp.net-mvc-routing

Aliasing a URL inside a custom ASP MVC Route

徘徊边缘 提交于 2019-12-11 03:40:00
问题 I'm creating URL aliasing functionality; the ability to map resource A to resource B. I'm fitting this into a project with a catch-all URL mapped to a custom Route that parses the incoming URL string and looks up the URL parts inside our database. Basically, the entire URL string is dynamic and maps to a category hierarchy (e.g. /tyres/car /tyres/van, /suspension/ford/focus). This is all working fine, and only exists to route to one controller. Currently I'm integrating my code for URL

asp.net mvc razor: How to directly access .cshtml page?

霸气de小男生 提交于 2019-12-11 03:34:19
问题 How can i directly access a .cshtml file in ASP.NET MVC using razor view engine? For example i have this url: localhost/Home/About. This will load the about site inside the "master" page. I want to load the about page without also loading the master page. So i was thinking that i could use this url: localhost/Home/About.cshtml. But it's not working. How can i load a view page without loading the master page? 回答1: You can use the method Html.Partial like this: @Html.Partial("About") Edit I

Routing ASP.NET MVC URL to url with querystring

夙愿已清 提交于 2019-12-11 03:28:40
问题 I have to map a nice looking ASP.NET MVC URL '/sel/F/61' to an ASPX url with query string parameters like 'familydrilldown.aspx?familyid=61'. I tried to add this to global.asax: routeTable.MapPageRoute( "selectorroute", "sel/F/{familyid}", "~/selector/familydrilldown.aspx"); which works, except that the familyid is not passed as a querystring to familydrilldown.aspx. How can I take the {familyid} and pass it as a querystring parameter to the page familydrilldown.aspx? I tried this: routeTable

Routing by slug in asp.net mvc

自作多情 提交于 2019-12-11 03:15:32
问题 I have a controller action as you can see below: public ActionResult Content(string slug) { var content = contentRepository.GetBySlug(slug); return View(content); } I want this kind of urls to be routed to my action: http://localhost/slug-of-my-content Here is my RegisterRoutes method: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller =

ASP.NET MVC routing - trying to have a name in the URL

…衆ロ難τιáo~ 提交于 2019-12-11 02:57:12
问题 I have currently the following routes: routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("{resource}.gif/{*pathInfo}"); MvcRoute.MappUrl("{controller}/{action}/{ID}") .WithDefaults(new { controller = "home", action = "index", ID = 0 }) .WithConstraints(new { controller = "..." }) .AddWithName("default", routes) .RouteHandler = new MvcRouteHandler(); MvcRoute.MappUrl("{title}/{ID}") .WithDefaults(new { controller = "special", action = "Index" }) .AddWithName("view", routes)

Is it possible to get route data on runtime outside of controller?

柔情痞子 提交于 2019-12-11 02:46:03
问题 I wonder if it's possible to somehow get the route value outside of controller? This is what I've tried. I also tried to do .Values["{projectId}"]; but I still get null . [RoutePrefix("projects")] public class UsergroupController : ApiController { [Route("{projectId}/usergroups"), HttpGet] public IHttpActionResult Get() { // Url: /projects/123/usergroups // Response: null var projectId = HttpContext.Current.Request.RequestContext.RouteData.Values["projectId"]; return Ok(projectId); } } I

How to get route name from action

♀尐吖头ヾ 提交于 2019-12-11 02:34:02
问题 I am using MapMvcAttributeRoutes to set routes in my MVC application. Now I have a doubt is it possible to access a route name from action Index? [HttpGet] [Route("{language}/Home/Index", Name = "Home.Index")] public ActionResult Index() { //access route name Home.Index here? return View(); } I wanted to say I tried something, but do not relly know where to start. I did some researching in forums and SO, but did not find any valid answer. EDIT In order to fully understand my question here is

Web Api Get() route not working when return type is HttpResponseMessage

强颜欢笑 提交于 2019-12-11 02:17:00
问题 This is causing me a bit of a headache. Ultimately, I'm trying to return an image but I've been experimenting and simplified this to using a string. What I want: go to URL: http://xxx/api/helloworld/1 Responds with: "Hello world!" The following web api declarations work for the url supplied; public string Get([FromUri]int id) { return "Hello World"; } public Task<string> Get([FromUri]int id) { return "Hello World"; } What does NOT work; public HttpResponseMessage Get([FromUri]int id) { return

Web API help page duplicating Action for all areas

安稳与你 提交于 2019-12-11 02:06:11
问题 I'm working with Web API 2, and it seems to pull up my existing API calls already, except it's duplicating all the calls for each area that i have. For example, say i have 3 areas, and in one of those i have an API call that looks like: public IList<string> GetStringList(string id) { //do work here... return new List<string>{"a","b","c"}; } if i have 3 areas, then the web api help page will show: GET area1/api/MyAPIController/GetStringList/{id} GET area2/api/MyAPIController/GetStringList/{id}

The best way to redirect all views to/from one view (e.g UnderConstructionView)

安稳与你 提交于 2019-12-11 01:37:20
问题 Is it possible to somehow route all views to one particular view? I would like to have an "Under Construction view" that is always the default view until i "flip a switch" without having to build. Until then all other controller action routes to this view. I would love to know if i can do it in web.config or if i have to have some if/else in RegisterRoutes in Global.asax. 回答1: you can write a custom filter attribute that reads a flag from web.config and redirect requests to under construction