asp.net-mvc-routing

MVC 3 Can't pass string as a View's model?

六眼飞鱼酱① 提交于 2019-12-02 21:33:51
I have a strange problem with my model passed to the View Controller [Authorize] public ActionResult Sth() { return View("~/Views/Sth/Sth.cshtml", "abc"); } View @model string @{ ViewBag.Title = "lorem"; Layout = "~/Views/Shared/Default.cshtml"; } The error message The view '~/Views/Sth/Sth.cshtml' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/Sth/Sth.cshtml ~/Views/Sth/abc.master //string model is threated as a possible Layout's name ? ~/Views/Shared/abc.master ~/Views/Sth/abc.cshtml ~/Views/Sth/abc.vbhtml ~/Views

Removing Controller from url in a specific scenario

送分小仙女□ 提交于 2019-12-02 21:10:33
问题 I want to remove Controller named Home from url when user clicks on About and Contact pages in ASP.NET MVC sample application . I tried this but it is giving me a 404 error. routes.MapRoute("Home", "{action}/{id}", new { controller = "Home" }); When i remove this all works perfectly. Note I want to remove Controller name only when Controller is Home. Other Controller should remain same. Further I kept the code in Route.Config file above routes.MapRoute("Home", "{action}/{id}", new {

ASP.NET MVC URL generation performance

半世苍凉 提交于 2019-12-02 20:47:54
A little benchmark with ASP.NET MVC. Viewpage code: public string Bechmark(Func<string> url) { var s = new Stopwatch(); var n = 1000; s.Reset(); s.Start(); for (int i = 0; i < n; i++) { var u = url(); } s.Stop(); return s.ElapsedMilliseconds + " ms, " + ((s.ElapsedMilliseconds) / (float)n) + " ms per link<br/>"; } View code: <%= Bechmark(() => Url.Action("Login", "Account")) %> <%= Bechmark(() => Url.Action("Login", "Account", new {username="bla", password="bla2", returnurl="blabla32", rememberme=false} )) %> <%= Bechmark(() => Html.BuildUrlFromExpression<AccountController>(a=>a.ChangePassword

ASP.NET MVC - MapRoute versus routes.Add (and 404s)

萝らか妹 提交于 2019-12-02 20:32:40
I'm just getting started with ASP.NET MVC. What is the difference between MapRoute and routes.Add ? Should I just be using MapRoute? Can I map multiple routes? Which "maps" take precedence... those you called first or last? I'd like to be able to do something similiar to the StackOverflow does for users. But I would like the URL to fit this pattern: "User/{domain}/{username}" to be routed to a UserController and for all other requests to do the typical ASP.NET MVC routing. ex: routes.MapRoute( "Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = "" } );

ASP.NET MVC Areas with shared layout

浪子不回头ぞ 提交于 2019-12-02 19:02:24
I have defined an area (Admin) in my ASP.NET MVC 3 application, created _ViewStart.cshtml in that area and added Layout = "~/Views/Shared/_Layout.cshtml"; to it to have a unified site layout. I also added the following code to _Layout.cshtml : if (HttpContext.Current.User.IsInRole("Admin")) { <li>@Html.ActionLink("Items List", "Index", "Items", new { area = "Admin" }, null)</li> } The Admin area is displayed properly having _Layout.cshtml as its layout. But all the navigation links in the page now point to Admin subfolder. For example in my layout I have <li>@Html.ActionLink("About Us", "About

Failing ASP.NET MVC route. Is this a bug or corner case?

谁都会走 提交于 2019-12-02 18:25:41
I have an ASP.NET MVC 3 application where users can post suggestions along the lines of "bla bla would be better if yada yada yada". For the suggestion detail page I have defined a nice SEO friendly route as follows: routes.MapRoute(null, "suggestion/{id}/{it}/would-be-better-if-{if}", new { controller = "suggestion", action = "details" }); As you can see I want the "would be better if" part to be fixed. This route works perfectly for any old suggestion and generates links like suggestion/5/this-site/would-be-better-if-it-had-a-iphone-application , and clicking on the link actually requests

Changing URL parameters with routing in MVC 4

安稳与你 提交于 2019-12-02 17:45:26
问题 Several of my API functions allow parameters called 'attribute' and 'attributeDelimiter' (in singular), meaning the expected URL would be in the format of SomeController/SomeAction?aaa=bbb&attribute=ccc&attributeDelimiter=ddd. I would like to allow support for plural in those param names as well - 'attributes' and 'attributesDelimiter'. Is there a way to re-write the url in the RouteConfig? (turning the plural names to singular) If that is not possible or it wouldn't be the best practice,

Custome MVC Url Rout to display mixture of Id and UrlSlug

只谈情不闲聊 提交于 2019-12-02 16:19:52
问题 Hi ' I'm gonna to apply the mixture of Id and slug as a url in my blog url like this http://stackoverflow.com/questions/16286556/using-httpclient-to-log-in-to-hpps-server to do this I have defined this Url in my global.asax routes.MapRoute("IdSlugRoute", "{controller}/{action}/{id}/{slug}", new {controller = "Blog", action = "Post", id = UrlParameter.Optional,slug=""}); but when I run my application Url is look like this : http://localhost:1245/Blog/Post?postId=dd1140ce-ae5e-4003-8090

mvc routing generates iis 7.5 error forbidden

北战南征 提交于 2019-12-02 15:03:28
问题 I my WebApplication I have an ASPX WebForms Page here: ~/ASPWebforms/MyFolder/Default.aspx If I use this code: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapPageRoute( "SomeRoute", "Test/{reportname}", "~/ASPWebforms/MyFolder/{reportname}.aspx" ); and then enter this in the browser: localhost/MySite/Test/Default I get the desired the result: The page ~/ASPWebforms/MyFolder/Default.aspx is displayed. But if I use the

Setup a route {tenant}/{controller}/{action}/{id} with ASP.NET MVC?

独自空忆成欢 提交于 2019-12-02 14:15:39
I would like to setup a multi-tenant ASP.NET MVC app. Ideally, this app would have a route with {tenant}/{controller}/{action}/{id} , each tenant representing an logical instance of the app (simply independent multi-user accounts) The fine grained details how do that are still quite unclear to me. Any guide available to setup such multi-tenant scheme with ASP.NET MVC? I am currently working on a similar project using ASP.Net MVC, Forms Authentication and the SQL providers for Membership/Roles/Profile. Here is the approach I am taking: Register the default route as `{tenant}/{controller}/