asp.net-mvc-routing

How to define Html.BeginForm for action with attribute routing?

。_饼干妹妹 提交于 2019-12-05 19:18:39
Controller [HttpGet] [Route("~/search/{clause}/{skip?}")] public async Task<ActionResult> Search(string clause, int skip = 0) { ... } View @using (Html.BeginForm("Index", "search", FormMethod.Get)) { @Html.TextBox("clause", null, new { @class = "form-control col-md-4" }) ... } Rendered Html <form action="/search" method="get"> <input id="clause" name="clause" type="text" value="test"> </form> I am using [HttpGet] partly because i want the search to be accessing via http://myapp.com/search/<search values> When i navigate to http://myapp.com/search/test , everything seems fine, but when i try to

Remove “api” prefix from Web API url

你说的曾经没有我的故事 提交于 2019-12-05 18:08:38
问题 I've got an API controller public class MyController : ApiController { ... } By default it is mapped to URL mysite/api/My/Method , and I'd like it to have URL without "api" prefix: mysite/My/Method Setting controller attribute [RoutePrefix("")] didn't help me. Are there any other ways to achieve that? 回答1: The default Registration is usually found in WebApiConfig and tends to look like this public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Attribute

Route value with dashes

一曲冷凌霜 提交于 2019-12-05 18:06:45
I have this route: routes.MapRoute( "News", "News/{id}-{alias}", new { controller = "News", action = "Show" }, new { id = @"^[0-9]+$" }, namespaces: new[] { "Site.Controllers" } ); This route working for url's like this: http://localhost:54010/News/6-news But not working for url's like this: http://localhost:54010/News/6-nice-news How use dashes in my route value "alias"? EDITED Route like this: "News/{id}_{alias}" works for both url's: http://localhost:54010/News/6_news http://localhost:54010/News/6_nice-news The problem is with your pattern: News/{id}-{alias} because the Routeing is parsing

How to bypass the exception multiple actions were found in ASP.NET Web API

杀马特。学长 韩版系。学妹 提交于 2019-12-05 16:13:40
When trying to find solution for this below question: MVC Web Api Route with default action not working Mostly I run across the problem "multiple actions were found". If routing mechanism finds out more than one actions matched with a route, it throws out the exception 500. For example, there are two actions in ValuesController: [HttpGet] public IEnumerable<String> Active() { var result = new List<string> { "active1", "active2" }; return result; } public IEnumerable<string> Get() { return new[] { "value1", "value2" }; } which match with default route: routes.MapHttpRoute( name: "DefaultApi",

Default-Route for root of application

穿精又带淫゛_ 提交于 2019-12-05 14:44:45
How can I tell my mvc -application to route to a specific Controller and Action when they are not specified? When debugging http://localhost:54500/ should route to http://localhost:54500/Home/Index . Currently I have: routes.MapRoute( name: "Root", url: "", defaults: new { controller = "Home", action = "Index" } ); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { id = UrlParameter.Optional } ); But this always throws The view 'Index' or its master was not found or no view engine supports the searched locations Edit #1 It should redirect/route to an View

Controller Action Methods with different signatures

╄→尐↘猪︶ㄣ 提交于 2019-12-05 14:25:58
I am trying to get my URLs in files/id format. I am guessing I should have two Index methods in my controller, one with a parameter and one with not. But I get this error message in browser below. Anyway here is my controller methods: public ActionResult Index() { return Content("Index "); } public ActionResult Index(int id) { File file = fileRepository.GetFile(id); if (file == null) return Content("Not Found"); else return Content(file.FileID.ToString()); } Update: Done with adding a route. Thanks to Jeff R0MANARMY You can only overload Actions if they differ in arguments and in Verb, not

Register routes in area registration

六眼飞鱼酱① 提交于 2019-12-05 11:22:17
I have two routes in my area, one custom and one default fallback route, see below var dashboardRoute = new DashboardRoute( ObjectFactory.GetInstance<PathResolver>(), ObjectFactory.GetInstance<VirtualPathResolver>(), null); context.Routes.Add(dashboardRoute); context.Routes.MapRoute( "Dashboard_Default", // Route name "dashboard/{controller}/{action}/{id}", // URL with parameters new { controller = "pages", action = "index", id = UrlParameter.Optional, area = "Dashboard" } // Parameter defaults ); when I add both routes using context.Routes.Add/MapRoute the last route is not working, but when

Permanent Redirect Legacy Routes for static files in ASP.Net MVC

北战南征 提交于 2019-12-05 11:17:41
Our old ASP.net site stored static images in a sub directory on the root called /images . Our new ASP.net MVC site stores these images in the new layout of /Content/Images I've changed all the pages in the site to cope with the new folder structure, but I'd like to set up Permanent Redirects from the old static images to the new location. Our site is hosted, and I don't have control over IIS, so what is the best approach to solve this? I use the following code for my MVC 2 websites: // The legacy route class that exposes a RedirectActionName public class LegacyRoute : Route { public

Task async controller method does not hit

痴心易碎 提交于 2019-12-05 08:36:45
So, we've got an MVC project that has been upgraded through the different versions of MVC from 1 through to 4. Now we have a controller method: public async Task<ActionResult> IndexAsync() so if we go to http://somedomain.xyz/WhicheverController or http://somedomain.xyz/WhicheverController/Index , we are greeted with a 404. http://somedomain.xyz/WhicheverController/IndexAsync routes to the method just fine. What's gone wrong with our routing? I believe that your example will work if you derive your Controller from AsyncController instead. public class MyController:AsyncController { public

Mixing ASP.NET MVC into ASP.NET WebForms

末鹿安然 提交于 2019-12-05 08:11:41
For some reason my routing is ignoring any attempt to access my MVC pages and simply giving me 404s. I have a WebForms app set up like the following: Virtual Directory: thing So I usually access my site like so: http://localhost/thing/someFile.aspx http://localhost/thing/someFolder/anotherFile.aspx The original stucture of my ASP.NET WebForms app mirrors the file system so I have folders full of .aspx files and I need to be able to use them like that. For some reason when I try to access a page using the MVC routing such as: http://localhost/thing/Home/Index I just get a 404 error. I have used