简体   繁体   English

使用LINQ查询单字符串导致路由

[英]Using LINQ query single string result in routing

I've now tried for two days by trial, error and google to do the following thing: 我现在已经尝试了两天,分别经过尝试,错误和Google,以执行以下操作:

When an user enters my MVC site, it loggs with its Windows account. 当用户进入我的MVC站点时,它将使用其Windows帐户登录。 It's working properly. 运作正常。 When he does that, MVC connects to db to extract his name (based on his account), which also works well. 当他这样做时,MVC连接到db以提取他的名字(基于他的帐户),这也很好用。 My task is to make my Index page to contain filtered information by default. 我的任务是使“索引”页面默认包含过滤的信息。 But user must also be able to change these filters by himself. 但是用户还必须能够自己更改这些过滤器。 My site's search functions are working well, using parameters from the url. 使用url中的参数,我的网站的搜索功能运行良好。 I tried to edit my routing by directly typing the username and then by setting it to Parameter.Optional. 我试图通过直接输入用户名,然后将其设置为Parameter.Optional来编辑路由。 Both methods result in errors: 两种方法都会导致错误:

public class RouteConfig
{
    public static CaseDBContext db = new CaseDBContext();
    public static string UName()
    {
        return db.Users.AsQueryable().Where(col => col.UNAD == HttpContext.Current.User.Identity.Name.ToString()).Select(col => col.Users).ToList().SingleOrDefault().ToString();
    }
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "",
            url: "{controller}/{action}/{dropDownIssBy}",
            defaults: new { controller = "Cases", action = "Index", dropDownIssBy = UName(), caseStatus = UrlParameter.Optional, sortBy = UrlParameter.Optional }
        );

This one ends in null reference exception. 这以null引用异常结束。 If I use new Func(UName), the site loads but when debugged it doesn't fire UName function at all and just ignores it. 如果我使用新的Func(UName),则将加载站点,但是在调试时,它根本不会触发UName函数,而只会忽略它。

private CaseDBContext db = new CaseDBContext();
public string UName()
{
return db.Users.AsQueryable().Where(col => col.UNAD == User.Identity.Name.ToString()).Select(col => col.Users).ToList().SingleOrDefault().ToString();
}
    public ActionResult Index(int? page, [...parameters...], string caseStatus = "In Progress", string dropDownIssBy = UName(), string SortBy = "Issue Date asc")
{
code here
}

The upper results in "Default parameter value must be compile-time constant. “默认参数值”的高位结果必须是编译时常数。

I also tried this: 我也试过这个:

public static string UName(string i)
    {
        string str = db.Users.AsQueryable().Where(col => col.UNAD == i).Select(col => col.Users).ToList().SingleOrDefault().ToString();
        return str;
    }
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "",
            url: "{controller}/{action}/{dropDownIssBy}",
            defaults: new { controller = "Cases", action = "Index", dropDownIssBy = UName(HttpContext.Current.User.Identity.Name.ToString()), caseStatus = UrlParameter.Optional, sortBy = UrlParameter.Optional }
        );

Result is "Object reference not set to an instance of an object." 结果是“对象引用未设置为对象的实例”。

FYI: The first code is connected to the first mentioned tryout. 仅供参考:第一个代码连接到第一个提到的试用。 The second code relies on different RouteConfig settings(instead of string dropDownIssBy = UName it is string dropDownIssBy = Parameter.Optional). 第二个代码依赖于不同的RouteConfig设置(不是字符串dropDownIssBy​​ = UName,而是字符串dropDownIssBy​​ = Parameter.Optional)。

I'm not good at these routing problems. 我不太擅长解决这些路由问题。 I really need to apply the LINQ query to achieve the desired functionality. 我确实需要应用LINQ查询以实现所需的功能。 Could you please give me some advice how to solve the problem? 您能给我一些解决问题的建议吗?

Thanks in advance! 提前致谢!

You're trying to assign the method UName to string dropDownIssBy (rather than it's result) in both cases. 在两种情况下,您都试图将方法UName分配给字符串dropDownIssBy​​(而不是结果)。 You missed off your brackets. 您错过了括号。 ie use UName() 即使用UName()

The lines in question should read: 有问题的行应显示为:

defaults: new { controller = "Cases", action = "Index", dropDownIssBy = UName(), caseStatus = UrlParameter.Optional, sortBy = UrlParameter.Optional }

and

public ActionResult Index(int? page, [...parameters...], string caseStatus = "In Progress", string dropDownIssBy = UName(), string SortBy = "Issue Date asc")

If you did intend passing a method then the syntax for the first line would look more like: 如果您确实打算传递一个方法,那么第一行的语法将更像:

defaults: new { controller = "Cases", action = "Index", dropDownIssBy = new Func<string>(UName), caseStatus = UrlParameter.Optional, sortBy = UrlParameter.Optional }

For the second line I don't believe you can set a default method for an optional parameter. 对于第二行,我认为您不能为可选参数设置默认方法。 You could get around this by having the default as null, and call UName from within the method if it is null. 您可以通过将默认值设置为null来解决此问题,并在方法为null时从方法中调用UName。 eg 例如

public ActionResult Index(int? page, [...parameters...], string caseStatus = "In Progress", Func<string> dropDownIssBy = null, string SortBy = "Issue Date asc")
{
    if(dropDownIssBy == null)
    {
        dropDownIssBy = UName;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM