标签: asp.net-mvc-routing

嵌套文件夹中的自定义ASP.NET MVC路由

我想在我的MVC应用程序中使用子文件夹,因此当前路由不会削减它.

我有一个文件夹结构,如

Views/Accounts/ClientBalances/MyReport.aspx
Run Code Online (Sandbox Code Playgroud)

我想要一个像这样的URL http://myapp/Accounts/ClientBalances/MyReport.你如何通过映射路线实现这一目标?我有一个bash,但我对他们不是很精明.我以为它是沿着的

 routes.MapRoute( _
        "Accounts/ClientBalances", _
        "Accounts/ClientBalances/{controller}/{action}/{id}", _
        New With {.controller = "Home", .action = "Index", .id = ""} _
    )
Run Code Online (Sandbox Code Playgroud)

我没有运气.有任何想法吗?

asp.net-mvc-routing

6
推荐指数
1
解决办法
3260
查看次数

System.Web.Routing.RouteCollection.GetRouteData中的异常

我在iis7上运行的asp.net mvc代码中随机获得了两个异常:

Exception type: InvalidOperationException 
Exception message: Collection was modified; enumeration operation may not execute. 
   at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   at System.Collections.Generic.List'1.Enumerator.MoveNextRare()
   at System.Collections.Generic.List'1.Enumerator.MoveNext()
   at System.Web.Routing.RouteCollection.GetRouteData(HttpContextBase httpContext)
   at System.Web.Routing.UrlRoutingModule.PostResolveRequestCache(HttpContextBase context)
   at System.Web.Routing.UrlRoutingModule.OnApplicationPostResolveRequestCache(Object sender, EventArgs e)
   at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Run Code Online (Sandbox Code Playgroud)

Exception type: NullReferenceException 
Exception message: Object reference not set to an instance of an object. 
   at System.Web.Routing.RouteCollection.GetRouteData(HttpContextBase httpContext)
   at System.Web.Routing.UrlRoutingModule.PostResolveRequestCache(HttpContextBase context)
   at System.Web.Routing.UrlRoutingModule.OnApplicationPostResolveRequestCache(Object sender, EventArgs e)
   at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Run Code Online (Sandbox Code Playgroud)

它不是一贯可重复的,但我认为它是改变(或腐败)的东西RouteTable.Routes.我RouteTable.Routes在项目中访问的唯一地方是Global.asax.cs …

c# asp.net-mvc iis-7 iis-6 asp.net-mvc-routing

6
推荐指数
1
解决办法
2976
查看次数

枚举ASP.NET MVC RouteTable路由URL

我试图找出如何枚举的网址RoutesRouteTable.

在我的场景中,我定义了以下路由:

routes.MapRoute
  ("PadCreateNote", "create", new { controller = "Pad", action = "CreateNote" });
routes.MapRoute
  ("PadDeleteNote", "delete", new { controller = "Pad", action = "DeleteNote" });
routes.MapRoute
   ("PadUserIndex", "{username}", new { controller = "Pad", action = "Index" });
Run Code Online (Sandbox Code Playgroud)

换句话说,如果我的网站是mysite.com,mysite.com/create会调用PadController.CreateNote(),而mysite.com/foobaris会调用它PadController.Index().

我还有一个强类型用户名的类:

public class Username
{
    public readonly string value;

    public Username(string name)
    {
        if (String.IsNullOrWhiteSpace(name)) 
        {
            throw new ArgumentException
                ("Is null or contains only whitespace.", "name");
        }

        //... make sure 'name' isn't a …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc asp.net-mvc-routing

6
推荐指数
1
解决办法
3237
查看次数

搜索页面MVC路由(隐藏动作,没有斜线,如SO)

我希望我的搜索类似于Stack Overflow中的搜索(即没有动作,没有斜线):

mydomain.com/search                        --> goes to a general search page  
mydomain.com/search?type=1&q=search+text   --> goes to actual search results  
Run Code Online (Sandbox Code Playgroud)

我的路线:

routes.MapRoute(  
  "SearchResults",  
  "Search/{*searchType}",      --> what goes here???  
  new { action = "Results" }  
);  
routes.MapRoute(  
  "SearchIndex",  
  "Search",  
  new { action = "Index" }  
);  
Run Code Online (Sandbox Code Playgroud)

我的SearchController有以下动作:

public ActionResult Index() { ... }  
public ActionResult Results(int searchType, string searchText) { ... }  
Run Code Online (Sandbox Code Playgroud)

搜索结果路线不起作用.我不想使用每个人似乎都在使用的".../..."方法,因为搜索查询不是资源,所以我希望查询字符串中的数据如我所指出的那样,没有斜线 - 就像SO一样.

TIA!马特

asp.net-mvc asp.net-mvc-routing

6
推荐指数
1
解决办法
794
查看次数

传递给Html.ActionLink时,在模型上序列化IList属性

我正在尝试使用以下viewmodel生成Html.ActionLink:

public class SearchModel
{
    public string KeyWords {get;set;}
    public IList<string> Categories {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

要生成我的链接,我使用以下调用:

@Html.ActionLink("Index", "Search", Model)
Run Code Online (Sandbox Code Playgroud)

其中Model是SearchModel的一个实例

生成的链接是这样的:

http://www.test.com/search/index?keywords=bla&categories=System.Collections.Generic.List

因为它显然只是在每个属性上调用ToString方法.

我想看到的是:

http://www.test.com/search/index?keywords=bla&categories=Cat1&categories=Cat2

有什么方法可以通过使用来实现这一点 Html.ActionLink

asp.net-mvc asp.net-mvc-routing

6
推荐指数
1
解决办法
917
查看次数

MVC传递id由"+"分隔为动作

我希望有可能通过以下URL类型访问操作:

http:// localhost/MyControllerName/MyActionName/Id1 + Id2 + Id3 + Id4

并按以下方式在代码中处理它:

public ActionResult MyActionName(string[] ids)
{
  return View(ids);
}
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc asp.net-mvc-routing asp.net-mvc-3

6
推荐指数
2
解决办法
108
查看次数

拾取错误的路由并且ActionLink生成错误的超链接

我是ASP.NET MVC3的新手.

我在Global.asax中配置了一些路由,我使用@ Html.ActionLink帮助方法生成一些超链接.

除了以下代码中的顶部链接之外,所有链接都正确呈现:

Global.asax中

routes.MapRoute(
    null,
    "Section/{Page}/{SubPage}/{DetailPageName}",
    new { controller = "Base" }
    );

routes.MapRoute(
    null,
    "Section/{Page}/{SubPage}",
    new { controller = "Base", action = "SubPage" }
    );

routes.MapRoute(
    null,
    "Section/{Page}",
    new { controller ="Base", action="LandingPage"}
    );

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}", // URL with parameters
    new { controller = "Base", action = "Index" } // Parameter defaults
    );
Run Code Online (Sandbox Code Playgroud)

ActionLink代码

@Html.ActionLink(@subPages.LinkedPageName, "DetailPage",
    new {
        Controller = "Base",
        Page = @ViewBag.PageName,
        SubPage = @Model.SubPageName,
        DetailPageName = subPages.LinkedPageName
    })
Run Code Online (Sandbox Code Playgroud)

以上应选择最佳路线,即: …

asp.net-mvc-routing

6
推荐指数
1
解决办法
573
查看次数

Asp.net MVC/Web Api路由:需要路由一点不同

我使用路由设置了一个asp.net web api项目(它与Mvc项目完全相同) - 因此我有以下内容

routes.MapHttpRoute(
    name: "API Default",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)

所以一切都按照我想要的方式工作....我输入api/Products/15它到达我的产品控制器,传入15作为id.

大.

我还有2个控制器,1个叫做UploadsController,1个叫做DownloadController.这些提供上传和下载(GET/PUT等)

现在我不希望他们被原规则接受(见上文)

但我想要的是使用这两个网址来访问它们

/ api/Transport/Uploads/15/api/Transport/Downloads/15

我已经通过了15作为身份证,可能不会发生在现实生活中...只是它有利于示范:-)

现在运输不存在所以我可以做以下事情

routes.MapHttpRoute(
    name: "API Default",
    routeTemplate: "api/Transports/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)

我认为有效...

但问题是,如果我这样做

/ api/Uploads/15 - 这也会被我不想要的原始规则所捕获..

我希望通过虚假的"传输"访问上传和DOwnloads控制器,而不是没有传输

有人可以帮忙吗?

提前致谢

asp.net-mvc asp.net-routing asp.net-mvc-routing asp.net-web-api

6
推荐指数
1
解决办法
3608
查看次数

在asp.net mvc中的redirecttoaction中设置查询字符串

我必须redirecttoaction在asp.net mvc视图中调用不同的参数,从视图的referrer页面(网格的状态)中提取.

我(在隐藏字段中)查询字符串的内容(有时是空的,有时带有2个参数等等),所以我在创建路由值数组时遇到问题.

是否有一些助手,帮助我将查询字符串转换为路由值数组?就像是:

string querystring ="sortdir=asc&pag=5";
return RedirectToAction( "Index", ConvertToRouteArray(querystring));
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc redirecttoaction asp.net-mvc-routing query-string

6
推荐指数
1
解决办法
9380
查看次数

基于GET变量的不同MVC4动作

有没有办法让MVC4根据URL中的GET变量调用不同的操作?

例如,假设我有以下两个动作.

[HttpPost]
public ActionResult SubmitCrash(CrashReport rawData)
{
  return View();
}


[HttpPost]
public ActionResult SubmitBug(BugReport data)
{
  return View();
}
Run Code Online (Sandbox Code Playgroud)

有没有办法让我可以使用以下URL让MVC4'选择'要调用哪个动作?

http://MySite/Submit?Crash (calls 'SubmitCrash')  
http://MySite/Submit?Bug (calls 'SubmitBug')
Run Code Online (Sandbox Code Playgroud)

更新:
我非常清楚我可以按照它们的方式使用动作/网址,并使用路由来实现它(这就是我现在正在做的事情),但我真的对GET vars部分感兴趣题.

c# asp.net-mvc-routing asp.net-mvc-4

6
推荐指数
1
解决办法
224
查看次数