asp.net-mvc-routing

How to support both kinds of URLs

核能气质少年 提交于 2020-01-24 12:15:08
问题 I'd like to be able to call GET by both: /api/Test/Test/1 /api/Test/Test?id=1 public class TestController : ApiController { [HttpGet] public string Test(int id) { return "Test"; } } How to configure the route? I have the default: config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); This makes "/api/Test/Test?id=1" work but not "/api/Test/Test/1" I tried to add the attribute

MVC Url Routing For custom url

萝らか妹 提交于 2020-01-24 12:15:07
问题 I want to pass url link like http://localhost:24873/Jobs/[companyname] or http://localhost:24873/[companyname]/Jobs/ (Preferred) I tried below routing in global aspx file and created controller named Jobs and Index action result with Jobs folder but not working. routes.MapRoute( "JobList", // Route name "Jobs/{companyname}", new { controller = "Jobs", action = "Index", companyname = string.Empty } ); And my controller: public partial class JobsController : Controller { public ActionResult

Subfolder in Controllers ASP.NET MVC [duplicate]

眉间皱痕 提交于 2020-01-21 08:35:31
问题 This question already has answers here : Route controller that is in a sub folder (2 answers) Closed 4 years ago . In my Controllers folder i want to have a subfolder called Admin. When i go to http://localhost:port/Admin/Login/ it says the page could not be found. RouteConfig.cs using System.Web.Mvc; using System.Web.Routing; namespace ICT4Events { public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes

How to determine if an arbitrary URL matches a defined route

自古美人都是妖i 提交于 2020-01-19 05:16:07
问题 How can I tell if a string matches a particular named route? I have a route like this: routes.MapRoute( "FindYourNewRental", "find-your-new-rental/{market}/{community}.html", new { controller = "FindYourNewRental", action = "Community" } ); string url = "http://www.website.com/find-your-new-rental/northerncalifornia/sacramento.html" How can I programmatically tell if the 'url' string matches that route? Something like this: // matches url with the named route "FindYourNewRental" if

Advanced ASP.NET MVC routing scenario

邮差的信 提交于 2020-01-16 08:35:23
问题 I have an ASP.NET MVC app with the following deployment requirements: The URL structure must be something like: http://server/app/[enterprise]/[communinty]/{controller}/{action}/... What I think I want to be able to do is intercept the URL before the MVC route handler gets its hands on it, remove the [enterprise]/[community] parts, and then allow MVC to continue processing as if the original URL had not contained those two segments. Here's why: The application exposes multiple portals to

MVC Routing - changing routes

廉价感情. 提交于 2020-01-16 01:54:07
问题 Using MVC4, I have the following routing for a blog post detail action which is SEO friendly URLS: public ActionResult Details(int id, string postName) { BlogPost blogPost = _blogService.GetBlogPostById(id); string expectedName = blogPost.Title.ToSeoUrl(); string actualName = (postName ?? "").ToLower(); if (expectedName != actualName) return RedirectToAction("Details", "Blog", new { id = blogPost.BlogPostId, postName = expectedName }); var vm = BuildBlogPostDetailViewModel(id); return View(vm

Route for favicon

心不动则不痛 提交于 2020-01-15 12:16:30
问题 I have a .net mvc4 application that has a multitentant solution. The way this is set up is that it tries to get a file for the client, and if not found it takes the global file. All this works great and in my RouteConfig.cs I have this line at the very bottom: routes.Add("FaviconRoute", new Route("favicon.ico", new StaticFileRouteHandler("favicon.ico", "Content", "", true))); StaticFileRouteHandler is my custom class that loads either global or tenant file and that works great. However I

MVC - Route with querystring

风流意气都作罢 提交于 2020-01-15 05:26:07
问题 After following this MVC 4 tutorial series I was trying some of the stuff myself. I started by trying to make the searchfilter url friendly. The following code is what I'm using at the moment: Global.asax public static void RegisterRoutes(RouteCollection routes) { routes.MapRoute( name: "MovieSearch", url: "Movies/SearchIndex/{movieGenre}/{searchString}", defaults: new { controller = "Movies", action = "SearchIndex", movieGenre = UrlParameter.Optional, searchString = UrlParameter.Optional } )

Hide Area in URL using AttributeRouting

走远了吗. 提交于 2020-01-14 13:37:49
问题 We are using Areas to version an API written in ASP.NET MVC3 with AttributeRouting to define our routes. Right now we have a "v1" area that is our first version of API. When we got to v2, we will copy over v1 and make modifications. I want to use the same versioning for a website and I don't want the /v1 in the route. My question is, how do I hide the Area in my URL so I can call mywebsite.com/Users/1 instead of mywebsite.com/v1/Users/1 Here is what I have in my controller [RouteArea("/")]

ASP.NET MVC Routing , Html.BeginForm

半城伤御伤魂 提交于 2020-01-14 13:31:49
问题 <% using (Html.BeginForm("SearchByZip", "Dealer", new { zip = ""}, FormMethod.Get)) { %> <div> <input type="text" class="padLeft" name="Zip" id="Zip" style="width: 200px" /> <input type="submit" class="btnFind" value="Find" /> </div> <% } %> This gives me the url "Dealer/SearchByZip?Zip=12345" I would like to end up with this: "Dealer/Zip/12345" (if I manually type in the url "Dealer/Zip/12345" it returns the right results, but when I click in submit it comes up with "Dealer/SearchByZip?Zip