目前我正在使用自定义类将路径转换为小写变体,这样可以毫无障碍地工作
防爆.〜/服务/餐饮=>〜/服务/餐饮
我的问题是,如果我用一个更像slug的设置替换Pascal Case,我怎样才能让MVC正确解析一个URL
防爆.〜/ Services/FoodAndDrink =>〜/服务/食品和饮料
我在继承的Route类中生成URL,重写GetVirtualPath()函数以转换为小写并用短划线和小写变体替换大写字母.
我想我会拦截URL和路由实际发生之前只是删除破折号,但我不知道在哪里这个下降的MVC页面周期
过去几天我一直在网上寻找解决方案,而且我找不到多少.希望我没有使用正确的术语,这是一件容易的事情.
我想使用如下路径:
/{projectId}
Run Code Online (Sandbox Code Playgroud)
并且在生命周期的早期某处可以访问路径值字典,我可以查询数据库或会话对象以获取用于此请求的控制器名称.然后,能够指定要使用的控制器, route.Values["controller"] = controllerName;并通过该控制器与请求参数的所有爵士乐等进行请求.
可能吗?
我目前正在使用区域,并且有以下路径:
/ProjectType1/{projectId}
/ProjectType2/{projectId}
Run Code Online (Sandbox Code Playgroud)
但是我觉得处理所有人的领域Html.Link并且讨厌为每个项目类型定义新区域真的很头疼.我很想找到更有活力的东西.
model-view-controller asp.net-mvc asp.net-mvc-routing asp.net-mvc-areas
我有一个控制器列出如下:
//
// GET: /Customer/Details/5
public ActionResult Details(short id)
{
ActionResult actionResult = null;
if (HttpContext.User.IsInRole("Admin"))
{
// this is the logic that is getting executed
YeagerTechWcfService.Customer cust = db.GetCustomerID(Convert.ToInt16(id));
actionResult = View("Details", cust); }
else
{
HttpCookie cn = Request.Cookies["strCookieName"];
if (cn != null)
{
YeagerTechWcfService.Customer cust = db.GetCustomerID(Convert.ToInt16(id));
actionResult = View("Details", cust);
}
else
{
TempData["ErrCode"] = "CustView";
actionResult = RedirectToAction("Index", "Home");
}
}
return actionResult;
}
Run Code Online (Sandbox Code Playgroud)
我有一个View(ActionLink所在的位置),如下所示:
columns.Template(
@<text>
@Ajax.ActionLink("Detail", "Details", "Customer", new { id = item.CustomerID …Run Code Online (Sandbox Code Playgroud) 我目前有一个ASP .NET MVC站点,有两个指向它的域(例如www.domainA.com和www.domainB.com).即使访问www.domainB.com,我也想更改所有链接以使用www.domainA.com.
我的所有网址都是由内置的Url.Action和Html.ActionLink方法生成的.
我按照上一个问题中的说明进行操作,但我无法完成它.
这是我的自定义路线:
public class MultipleDomainRoute : System.Web.Routing.Route
{
public MultipleDomainRoute(string url, IRouteHandler routeHandler)
: base(url, routeHandler)
{
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
VirtualPathData path = new VirtualPathData(this, "http://www.domainA.com/" + values["controller"] + "/" + values["action"]);
return path;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的自定义路由处理程序:
class MyRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return new MvcHandler(requestContext);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的RegisterRoutes方法:
public static void RegisterRoutes(RouteCollection routes)
{
routes.Add(new MultipleDomainRoute("{controller}/{action}/{id}", new MyRouteHandler()));
routes.MapRoute(
"Default", // Route …Run Code Online (Sandbox Code Playgroud) 尝试在Visual Studio 2010中使用MVC RC4 Web API设置新站点,它似乎不起作用:参数值永远不会传递给方法.
在mvc2中一切都运行良好,但自从升级到mvc 4后,大部分事情开始崩溃.
public ActionResult DownloadApp(string id, bool download = false){}
Run Code Online (Sandbox Code Playgroud)
我试图传递下载参数,但控制器没有捕获它
RedirectResult (Url.Action("DownloadApp", "Account") + "?download=true");
Run Code Online (Sandbox Code Playgroud)
就像我刚才提到的,它在MVC 2中运行良好.我被迫使用Queryparameter我不想使用的.有没有办法让它像以前一样在MVC 4中运行.路线如下:
RouteTable.Routes.MapRoute(
"Default",
"api/{controller}/{action}/{id}",
new { controller = "account", action = "index", id = "" },
namespaces
);
Run Code Online (Sandbox Code Playgroud) 给定路由名称,我可以获取默认操作名称和控制器名称吗?
routes.MapRoute(
name: "MonthlyCalendar",
url: "FMB/Calendar/{jamaatName}/{year}/{month}",
defaults: new { controller = "FMB", action = "MonthlyCalendar",
jamaatName = UrlParameter.Optional,year = UrlParameter.Optional,
month=UrlParameter.Optional}
);
Run Code Online (Sandbox Code Playgroud) 在HomeController.cs我有:
[HttpGet]
public GetPerson(string name)
{
return View(new PersonModel { ... });
}
Run Code Online (Sandbox Code Playgroud)
在Global.asax.cs我有:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Word", "person/{name}",
new { controller = "Home", action = "GetPerson" });
routes.MapRoute(
"Default", "{controller}/{action}",
new { controller = "Home", action = "Index" });
}
Run Code Online (Sandbox Code Playgroud)
在SomePage.cshtml我有,有效的做法是:
@{ var name = "Winston S. Churchill"; }
<a href="@Url.Action("GetPerson", "Home", new { name })">@name</a>
Run Code Online (Sandbox Code Playgroud)
如果单击Winston S. Churchill的链接,则会被定向到URL http://localhost/person/Winston%20S.%20Churchill,该URL 产生标准的404页面:
HTTP错误404.0-找不到
您要查找的资源已被删除,名称已更改或暂时不可用。
仅当name …
我有登录控制器和注册ActionResult
public ActionResult Register()
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
如果我发布任何数据到注册actionresult网址如下所示,
WebSiteName/Login/Register
Run Code Online (Sandbox Code Playgroud)
我想将路由网址更改为WebSiteName/Login/Reg = Id?
所以我尝试下面,但我无法改变路线网址.
routes.MapRoute(
name: "something",
url: "Login/Reg=Id?",
defaults: new
{
controller = "Login",
action = "Register",
id = id = UrlParameter.Optional
}
);
Run Code Online (Sandbox Code Playgroud)
那么如何在asp.net mvc中更改url?
任何帮助将不胜感激.
谢谢.
我刚刚开始一个新的MVC项目,默认路由似乎没有像我期望的那样工作.
路由配置:
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 }
);
}
Run Code Online (Sandbox Code Playgroud)
我ActionLink在"Scripts"控制器上进行了"索引"操作@Html.ActionLink("Scripts", "Index", "Scripts").
public class ScriptsController : Controller
{
public ActionResult Index()
{
var model = new IndexModel();
.......
return View(model);
}
}
Run Code Online (Sandbox Code Playgroud)
单击此链接时,浏览器会尝试导航到" http:// localhost:1733/Scripts / ",浏览器会显示"目录列表被拒绝"错误.当我手动将URL更改为" http:// localhost:1733/Scripts/Index "时,它可以正常工作.
默认路由不应自动推断"索引"操作吗?它似乎适用ManageController于作为默认MVC站点模板一部分的类(" http:// localhost:1733/Manage "会调出IndexActionResult ManageController)
我究竟做错了什么?
在ASP.NET MVC中,可以定义这样的路由:
routes.MapRoute("myroute",
"myroute/{country}/{name}-{type}",
new { controller = "MyController", action = "Get" });
Run Code Online (Sandbox Code Playgroud)
这会将它直接解析为一个对象:
public class MyController : Controller
{
public HttpResponseMessage Get([FromRoute] MyViewModel model)
{
//TODO do stuff with model.
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的视图模型:
public class MyViewModel
{
public string Name { get; set; }
public string Type{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,我可以在一个简单的控制台应用程序中进行相同的解析吗?
class Program
{
static void Main(string[] args)
{
string route = "myroute/{country}/{name}-{type}";
string input = "myroute/Denmark/MyName-MyType";
//TODO Parse input to MyViewModel with route
MyViewModel result;
}
} …Run Code Online (Sandbox Code Playgroud)