场景:我的ASP.NET MVC 5站点中有一个Forms区域.
我正在尝试重定向到Details Action,它使用使用新的Attribute Routing功能定义的自定义路由.
RedirectToAction:
return RedirectToAction("Details", new { slug });
Run Code Online (Sandbox Code Playgroud)
我正在重定向到的操作:
[HttpGet]
[Route("forms/{slug}")]
public ActionResult Details(string slug)
{
var form = FormRepository.Get(slug);
...
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
我希望重定向,http://localhost/forms/my-slug但应用程序将重定向到我http://localhost/Forms/Details?slug=my-slug.
这意味着属性路由不起作用.
怎么解决这个问题?
我添加了routes.MapMvcAttributeRoutes(); 到我的RouteConfig:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的Application_Start():
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles); …Run Code Online (Sandbox Code Playgroud) 我最近在ASP.NET MVC 4中发现了我已经成功实现的区域,但是我@Html.ActionLink在Razor视图中遇到了帮助器的问题.此助手生成的URL似乎总是相对于当前页面的URL.
我将我的网站在IIS 7(Win7)中配置为应用程序虚拟目录/Foo.
我注册了两个区域:管理员和博客.我在AdminAreaRegistration.cs和BlogsAreaRegistration.cs文件中有默认代码.我添加namespaces = new[] { "MvcProject.Controllers" }到默认RouteConfig的默认值:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {
controller = "Home", action = "Index", id = UrlParameter.Optional,
namespaces = new[] { "MvcProject.Controllers" }
}
);
}
}
Run Code Online (Sandbox Code Playgroud)
当我转到我的网站的主页:时http://localhost/Foo,它正确加载我的网站的"主页"页面.此时,所有操作链接都有正确的URL.
示例代码来自 MvcProject/Views/Shared/_Layout.cshtml
<h2>Main Navigation</h2>
<ul>
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("Blogs", "Index", "Blogs/Home")</li>
<li>@Html.ActionLink("Admin", "Index", "Admin/Home")</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
这正确地将HTML呈现为: …
如何在ASP.NET MVC框架中生成友好的URL?例如,我们有一个如下所示的URL:
http://site/catalogue/BrowseByStyleLevel/1
1是研究级别的Id(在这种情况下是更高的)来浏览,但我想以与StackOverflow相同的方式重新格式化URL.
例如,这两个网址会将您带到同一个地方:
编辑:网址的友好部分被称为slug.
任何人都可以指出我正确的方向如何绘制一个需要两个guid的路线?
即.http://blah.com/somecontroller/someaction/ {firstGuid}/{secondGuid}
其中firstGuid和secondGuid都不是可选的,必须是system.Guid类型?
我在我的ASP.NET MVC 3应用程序中定义了一个区域(Admin),_ViewStart.cshtml在该区域中创建并添加Layout = "~/Views/Shared/_Layout.cshtml";到该区域以具有统一的站点布局.
我还将以下代码添加到_Layout.cshtml:
if (HttpContext.Current.User.IsInRole("Admin"))
{
<li>@Html.ActionLink("Items List", "Index", "Items", new { area = "Admin" }, null)</li>
}
Run Code Online (Sandbox Code Playgroud)
该Admin区域_Layout.cshtml以其布局正确显示.但是页面中的所有导航链接现在都指向Admin子文件夹.
例如,在我的布局中,我<li>@Html.ActionLink("About Us", "About", "Home")</li>指出了Mysite/Home/About.但点击管理链接后,"关于我们"链接指向/Admin/Home/About.
如果让_Layout.cshtml链接指向正确的地址,我该怎么办?
谢谢.
我正在研究'ASP.NET MVC 4'应用程序.我正在使用/学习SimpleMembershipProvider,并尝试坚持VS2012使用Internet template(如果我没有记错的话,创建的默认逻辑,开箱即用的'SimpleMembershipProvider').
我被困在AccountController我无法想象的地方,我怎么能使用这个方法:
private ActionResult RedirectToLocal(string returnUrl)
{
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
Run Code Online (Sandbox Code Playgroud)
根据我的理解,整个想法是重定向到您决定登录的位置(正是我想要完成的).我看了一下它在视图中的使用方式:
@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl }))
Run Code Online (Sandbox Code Playgroud)
寻找一个实际ViewBag.ReturnUrl设置了一些值的地方,我这里只有这个方法:
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
Run Code Online (Sandbox Code Playgroud)
而且我对我应该如何获得位置/网址感到非常困惑.我设置了一些断点,我从来没有见过returnUrl不同的东西null,在这种情况下对我来说似乎很合乎逻辑,因为它在任何地方都没有价值(除非我当然错过了一些东西).
所以我真的无法弄清楚这是如何工作的.我发布以上内容只是为了表明我试图做我的作业,我尽可能多地调查,但我没有找到答案所以我在这里问.你能提供关于它如何实际工作的解释/例子吗?
我需要一个实现,我可以在我的ASP.NET控制器上获得无限的参数.如果我举个例子,那会更好:
我们假设我将有以下网址:
example.com/tag/poo/bar/poobar
example.com/tag/poo/bar/poobar/poo2/poo4
example.com/tag/poo/bar/poobar/poo89
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,它将获得无限数量的标记,example.com/tag/并且斜杠将成为此处的分隔符.
在控制器上我想这样做:
foreach(string item in paramaters) {
//this is one of the url paramaters
string poo = item;
}
Run Code Online (Sandbox Code Playgroud)
有没有任何已知的方法来实现这一目标?如何从控制器获取值?用Dictionary<string, string>或List<string>?
注意 :
IMO没有很好地解释这个问题,但我尽力适应它.in.随意调整它
我有一个方法创建一个包含Error对象的HttpResponseMessage,该对象将根据当前请求媒体类型格式化程序返回.
目前,我已经对XmlMediaTypeFormatter进行了硬编码,但我希望能够在运行时找到当前请求MediaTypeFormatter,但由于我的下面代码存在于单独的类库中,因此我无法访问当前请求对象.
private HttpResponseMessage Create(HttpStatusCode statusCode, string errorCode, string errorMessage)
{
var result = new HttpResponseMessage(statusCode)
{
Content = new ObjectContent<Error>(new Error()
{
Code = errorCode,
Message = errorMessage
}, new XmlMediaTypeFormatter())
};
return result;
}
Run Code Online (Sandbox Code Playgroud)
如何全局访问当前的HttpRequestMessage对象?像HttpContext.Current.Request之类的东西
如果不可能,如何实现上述方法,以便它知道它应该用于当前请求的格式器?
我可以一起使用以下两条路线规则吗?
config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional } );
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
控制器说是= FruitApiController:ApiController并且我希望有以下内容
List<Fruit> Get() = api/FruitApi/
List<Fruit> GetSeasonalFruits() = api/FruitApi/GetSeasonalFruit
Fruit GetFruits(string id) = api/FruitApi/15
Fruit GetFruitsByName(string name) = api/FruitApi/GetFruitsByName/apple
请帮帮我.谢谢
asp.net-mvc asp.net-mvc-routing asp.net-mvc-4 asp.net-web-api
在视图内部,我可以获取动作的完整路线信息吗?
如果我在控制器MyController中有一个名为DoThis的动作.我可以走一条路"/MyController/DoThis/"吗?