标签: asp.net-mvc-routing

MVC 3无法将字符串作为View的模型传递?

传递给View的模型有一个奇怪的问题

调节器

[Authorize]
public ActionResult Sth()
{
    return View("~/Views/Sth/Sth.cshtml", "abc");
}
Run Code Online (Sandbox Code Playgroud)

视图

@model string

@{
    ViewBag.Title = "lorem";
    Layout = "~/Views/Shared/Default.cshtml";
}
Run Code Online (Sandbox Code Playgroud)

错误消息

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/Shared/abc.cshtml
~/Views/Shared/abc.vbhtml
Run Code Online (Sandbox Code Playgroud)

为什么我不能将简单的字符串作为模型传递?

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

64
推荐指数
4
解决办法
3万
查看次数

参数中包含斜杠的网址?

题:

我正在创建一个wiki软件,基本上是维基百科/ mediawiki的克隆,但是在ASP.NET MVC中(MVC是重点,所以不建议我使用ScrewTurn).

现在我有一个问题:

我使用此路由映射来路由URL,如:http:
//en.wikipedia.org/wiki/ASP.NET

        routes.MapRoute(
            "Wiki", // Routenname
            //"{controller}/{action}/{id}", // URL mit Parametern
            "wiki/{id}", // URL mit Parametern
            new { controller = "Wiki", action = "dbLookup", id = UrlParameter.Optional } // Parameterstandardwerte
        );
Run Code Online (Sandbox Code Playgroud)

现在它刚刚发生在我身上,可能会出现像'AS/400'这样的标题:http:
//en.wikipedia.org/wiki/AS/400

在整体上,还有这个(标题'Slash'):http:
//en.wikipedia.org/wiki//

这一个:http:
//en.wikipedia.org/wiki//dev/null

总的来说,维基百科似乎有一个有趣的标题列表,如:http://en.wikipedia.org/wiki/Wikipedia : Articles_with_slashes_in_title

如何正确制作此路线的路线?

编辑:
类似于:
如果URL以/ Wiki /开头,如果它不以/ wiki/Edit /(但不是/ Wiki/Edit)开头,则将所有其余URL作为Id传递.

编辑:
嗯,只是另一个问题:我如何路由这个:http:
//en.wikipedia.org/wiki/C&A

维基百科可以......

编辑:
根据维基百科,由于与wikitext语法的冲突,在页面标题中永远不能使用以下字符(DISPLAYTITLE也不支持):

# < > [ ] | { …
Run Code Online (Sandbox Code Playgroud)

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

63
推荐指数
3
解决办法
4万
查看次数

未找到"索引"视图或其主页.

The view 'Index' or its master was not found. The following locations were searched:
~/Views/ControllerName/Index.aspx
~/Views/ControllerName/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
Run Code Online (Sandbox Code Playgroud)

使用ASP.Net mvc区域时出现此错误.调用区域控制器操作,但它似乎在"基础"项目视图中而不是在区域视图文件夹中查找视图.

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

57
推荐指数
6
解决办法
10万
查看次数

URL中的URL编码斜杠

我的地图是:

routes.MapRoute(
   "Default",                                             // Route name
   "{controller}/{action}/{id}",                          // URL with params
   new { controller = "Home", action = "Index", id = "" } // Param defaults
);
Run Code Online (Sandbox Code Playgroud)

如果我使用URL http://localhost:5000/Home/About/100%2f200,则没有匹配的路由.我将URL更改为http://localhost:5000/Home/About/100然后再次匹配路由.

有没有简单的方法来处理包含斜杠的参数?其他转义值(空格%20)似乎有效.

编辑:

编码Base64对我有用.它使URL变得丑陋,但现在还可以.

public class UrlEncoder
{ 
    public string URLDecode(string  decode)
    {
        if (decode == null) return null;
        if (decode.StartsWith("="))
        {
            return FromBase64(decode.TrimStart('='));
        }
        else
        {
            return HttpUtility.UrlDecode( decode) ;
        }
    }

    public string UrlEncode(string encode)
    {
        if (encode == null) return null;
        string encoded = …
Run Code Online (Sandbox Code Playgroud)

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

56
推荐指数
5
解决办法
6万
查看次数

当ID包含句点时,ApiController返回404

我有一个ApiController,我想使用电子邮件地址作为请求的ID参数:

// GET api/employees/email@address.com
public CompactEmployee Get(string id) {
   var email = id;
   return GetEmployeeByEmail(email);
}
Run Code Online (Sandbox Code Playgroud)

但是,我不能让它工作(返回404):

http://localhost:1080/api/employees/employee@company.com

以下所有工作:

  • http://localhost:1080/api/employees/employee@company
  • http://localhost:1080/api/employees/employee@company.
  • http://localhost:1080/api/employees?id=employee@company.com

我已经设置relaxedUrlToFileSystemMapping="true"了我的web.config,详见Phil Haack.

我非常喜欢完整的电子邮件地址,但是任何时候任何其他角色跟着这个时期,请求都会返回404.任何帮助都将非常感谢!

由于缺乏其他选项,我已朝着Maggie建议的方向前进,并使用此问题的答案来创建重写规则,以便在我需要URL中的电子邮件时自动附加尾部斜杠.

<system.webServer>
  ....   
  <rewrite>
    <rules>
      <rule name="Add trailing slash" stopProcessing="true">
        <match url="^(api/employees/.*\.[a-z]{2,4})$" />
        <action type="Rewrite" url="{R:1}/" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

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

56
推荐指数
1
解决办法
1万
查看次数

如何从Child动作中获取当前控制器和动作?

我有一部分视图通过RenderAction调用子动作呈现.如何从此子操作中获取父控制器和操作.

我用的时候..

@ViewContext.RouteData.Values["action"]
Run Code Online (Sandbox Code Playgroud)

我找回了Child Action的名字,但我需要的是Parent/Calling动作.

谢谢

顺便说一下,我正在使用带Razor的MVC 3.

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

55
推荐指数
3
解决办法
3万
查看次数

ASP.NET MVC - 提取URL的参数

我正在尝试提取我的URL的参数,就像这样.

/行政/客户/编辑/ 1

提取物: 1

/管理/产品/编辑/ 18?允许=真

提取物: 18?allowed = true

/管理/产品/创建?允许=真

extract:?allowed = true

有人可以帮忙吗?谢谢!

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

55
推荐指数
2
解决办法
18万
查看次数

ASP.NET MVC 5文化路由和网址

我翻译了我的mvc网站,这个网站运行得很好.如果我选择其他语言(荷兰语或英语),内容将被翻译.这是有效的,因为我在会话中设置了文化.

现在我想在网址中显示所选文化(=文化).如果它是默认语言,则不应在网址中显示,只有当它不是默认语言时才应在网址中显示.

例如:

对于默认文化(荷兰语):

site.com/foo
site.com/foo/bar
site.com/foo/bar/5
Run Code Online (Sandbox Code Playgroud)

对于非默认文化(英语):

site.com/en/foo
site.com/en/foo/bar
site.com/en/foo/bar/5
Run Code Online (Sandbox Code Playgroud)

我的问题是我总是看到这个:

site.com/ nl/foo/bar/5即使我点击了英文(参见_Layout.cs).我的内容是用英文翻译的,但网址中的路由参数仍然是"nl"而不是"en".

我怎样才能解决这个问题或者我做错了什么?

我尝试在global.asax中设置RouteData但没有帮助.

  public class RouteConfig
  {
    public static void RegisterRoutes(RouteCollection routes)
    {
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
      routes.IgnoreRoute("favicon.ico");

      routes.LowercaseUrls = true;

      routes.MapRoute(
        name: "Errors",
        url: "Error/{action}/{code}",
        defaults: new { controller = "Error", action = "Other", code = RouteParameter.Optional }
        );

      routes.MapRoute(
        name: "DefaultWithCulture",
        url: "{culture}/{controller}/{action}/{id}",
        defaults: new { culture = "nl", controller = "Home", action = "Index", id = UrlParameter.Optional },
        constraints: new { culture = "[a-z]{2}" } …
Run Code Online (Sandbox Code Playgroud)

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

50
推荐指数
2
解决办法
3万
查看次数

如何在ASP.NET Core中使用Area

如何Area在ASP.NET Core中使用?

我有一个需要管理员部分的应用.此部分要求将其视图放置在该区域中.所有以请求开头的请求Admin/都需要重定向到该区域.

asp.net-mvc-routing asp.net-mvc-areas asp.net-core-mvc asp.net-core

50
推荐指数
4
解决办法
3万
查看次数

找不到与名为"User"的控制器匹配的类型

我正在尝试导航到一个页面,其URL的格式如下:localhost:xxxxx/User/{id}/VerifyEmail?secretKey = xxxxxxxxxxxxxxx

我在RouteConfig.cs文件中添加了一个新路由,所以我RouteConfig.cs看起来像这样:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "VerifyEmail",
            url: "User/{id}/VerifyEmail",
            defaults: new { controller = "User", action = "VerifyEmail" }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index",
                id = UrlParameter.Optional }
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,当我尝试导航到该URL时,我得到了这个页面:

<Error>
    <Message>
        No HTTP resource was found that matches the request URI 'http://localhost:52684/User/f2acc4d0-2e03-4d72-99b6-9b9b85bd661a/VerifyEmail?secretKey=e9bf3924-681c-4afc-a8b0-3fd58eba93fe'.
    </Message>
    <MessageDetail>
        No type was found that matches the controller …
Run Code Online (Sandbox Code Playgroud)

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

47
推荐指数
6
解决办法
7万
查看次数