我正在尝试在MVC中设置自定义路由,以便采用以下格式从另一个系统获取URL:
../ABC/ABC01?Key=123&Group=456
第二个ABC之后的01是一个步骤编号,这将改变,键和组参数将改变.我需要将此路由路由到控制器中的一个操作,步骤编号键和组作为参数.我尝试了以下代码,但它抛出异常:
码:
routes.MapRoute(
"OpenCase",
"ABC/ABC{stepNo}?Key={key}&Group={group}",
new {controller = "ABC1", action = "OpenCase"}
);
Run Code Online (Sandbox Code Playgroud)
例外:
`The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character.`
Run Code Online (Sandbox Code Playgroud) 基本上我想这样做:http: //website.com/Home/About
显示为:http: //website.com/About
显示在URL中的"home"控制器将使用户更长的URL读取.
我试着做以下事情:
routes.MapRoute(
name: "About",
url: "",
defaults: new { controller = "Home", action = "About", id = UrlParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
有人可以帮帮我吗?
我试图在我的ASP.net MVC4/WebApi项目中使用以下(和类似的)URL:
http://127.0.0.1:81/api/nav/SpotiFire/SpotiFire.dll
Run Code Online (Sandbox Code Playgroud)
负责此网址的路线如下所示:
config.Routes.MapHttpRoute(
name: "Nav",
routeTemplate: "api/nav/{project}/{assembly}/{namespace}/{type}/{member}",
defaults: new { controller = "Nav", assembly = RouteParameter.Optional, @namespace = RouteParameter.Optional, type = RouteParameter.Optional, member = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
如果我删除它,它的工作正常.在文件名中,或者如果我在URL后面添加斜杠,但这也意味着我不能使用Url.Route-methods等.我得到的错误是一般的404错误(下图).

我已经尝试添加<httpRuntime targetFramework="4.5" relaxedUrlToFileSystemMapping="true" />到我的web.config,我也尝试添加
<compilation debug="true" targetFramework="4.5">
<buildProviders>
<remove extension=".dll"/>
<remove extension=".exe"/>
</buildProviders>
</compilation>
Run Code Online (Sandbox Code Playgroud)
而且似乎没有任何效果.所以我的问题基本上是,如何让这个URL工作,并正确映射?
如何在ASP.NET MVC 4应用程序中添加路由到RouteConfig.cs文件以执行永久301重定向到另一个路由?
我想某些不同的路线指向相同的控制器动作 - 似乎301是最佳实践,特别是对于SEO?
谢谢.
这是我的情景.对于该示例,我们可以说我需要根据搜索条件返回汽车列表.我希望有一个View来显示结果,因为输出将是相同的,但我需要几种方法来实现.例如,我可能有一个带有文本框的表单按年搜索.我可能有另一个单独的页面,其中包含所有红色丰田汽车的超链接.如何在同一视图和控制器中处理这些多个方案.我的困境是搜索可能包含几个选项...年份,品牌,模型等,但我不知道在哪里放置它们.
对此最好的方法是什么?我应该在路由中定义参数还是使用查询字符串等?
假设您有一种在购物车中显示产品的操作方法
// ProductsController.cs
public ActionMethod Index(string gender) {
// get all products for the gender
}
Run Code Online (Sandbox Code Playgroud)
在其他地方,在您Url.RouteUrl用于创建到网站上其他页面的HREF链接的每个页面上显示的标头中:
<a href="<%= Url.RouteUrl("testimonials-route", new { }) %>" All Testimonials </a>
Run Code Online (Sandbox Code Playgroud)
这由下面的第一条路线testimonials-route定义global.ascx.请注意,上面的调用RouteUrl不提供a gender,但是路由定义为默认的"中性",所以我们期望调用Testimonials.Index("中性").
routes.MapRoute(
"testimonials-route",
"testimonials/{gender}",
new { controller = "Testimonials", action = "Index", gender = "neutral" },
new { gender = "(men|women|neutral)" }
);
routes.MapRoute(
"products-route",
"products/{gender}",
new { controller = "Products", action = "Index", gender = (string)null },
new { gender = "(men|women|neutral)" …Run Code Online (Sandbox Code Playgroud) 使用标准的MapRoute方法,a可以传递一个字符串集合,表示在其中搜索我的控制器的命名空间.这似乎从MapHttpRoute中消失了.如何使用新的API路由定义默认命名空间?
在以下链接的5分钟视频中,在1:10标记处,Jon Galloway说,在他的CommentsController控制器类中添加一个名为DeleteComment的方法将按照约定自动映射到删除http动词.
如何使用WebApi的MVC知道如何将方法路由到正确的动词?我知道global.asax.cs文件中的路由将请求路由到正确的控制器,但是删除请求如何"按惯例"映射到delete方法或任何方法?特别是当每个动词可以有多于1种方法时?"按照惯例"让我觉得它只是在查看方法名称中的第一个单词...但如果是这样的话,它必须读取方法的签名来告诉两个删除方法或两个方法分开...以及是这个定义了吗?
视频:http: //www.asp.net/web-api/videos/getting-started/delete-and-update
谢谢!
编辑:以下是WebApi模板中的示例ValuesController类中的代码.这是我原来问题的来源.区分这些(以及控制器中的任何其他方法)的"约定"如何工作?
// GET /api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET /api/values/5
public string Get(int id)
{
return value;
}
Run Code Online (Sandbox Code Playgroud) 使用提供的默认路由,我被迫将参数命名为"id".这对我的许多控制器动作来说都很好,但我想在某些地方使用一些更好的变量命名.我可以使用某种属性,以便在操作签名中包含更有意义的变量名称吗?
// Default Route:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
// Action Signature:
public ActionResult ByAlias(string alias)
{
// Because the route specifies "id" and this action takes an "alias", nothing is bound
}
Run Code Online (Sandbox Code Playgroud) 我有一个空的ASP.NET应用程序,我添加了一个index.html文件.我想将index.html设置为网站的默认页面.
我试图右键单击index.html并设置为起始页面,当我运行它时,url是:http://localhost:5134/index.html但我真正想要的是当我输入:时http://localhost:5134,它应该加载index.html页面.
我的路线配置:
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 }
);
}
}
Run Code Online (Sandbox Code Playgroud)