使用控制器内的参数值重定向到动作

时间:2019-11-14 22:55:41

标签: c# asp.net-mvc asp.net-core controller core

场景

我有一个身份验证控制器,用于授权用户和管理员凭据并注册新用户。当发现用户已经存在时,我想将控制器重新路由到我在控制器中声明的重载动作,并将字符串作为“路由值”传递给该动作。但是,目前看来这行不通。

AuthController.cs

    [HttpGet]
    public IActionResult Register()
    {
        ViewData["Title"] = "Registration";

        return View(new RegisterViewModel());
    }

    public IActionResult Register(string caption)
    {
        ViewData["Title"] = "Registration";

        ViewBag.caption = caption;

        return View(new RegisterViewModel());
    }

    [HttpPost]
    public async Task<IActionResult> Register(RegisterViewModel vm)
    {
        if (!ModelState.IsValid)
            return View(vm);

        var user = new IdentityUser
        {
            UserName = vm.Email,
            Email    = vm.Email
        };

        var result = await _userManager.CreateAsync(user, "password");

        if (result.Succeeded)
        {
            // !> Initial Registration!
            await _signInManager.SignInAsync(user, false);
            return RedirectToAction("Index", "Home");
        }

        return RedirectToAction("Register", "Auth", "This user is currently configured with a password");
    }

问题

当前是否使用以下重定向:

    return RedirectToAction("Register", "Auth", "This user is currently configured with a password");

它转到第一个动作或该动作:

    public IActionResult Register()
    {
        ViewData["Title"] = "Registration";

        return View(new RegisterViewModel());
    }

而不是第二个或该操作:

    public IActionResult Register(string caption)
    {
        ViewData["Title"] = "Registration";

        ViewBag.caption = caption;

        return View(new RegisterViewModel());
    }

不确定我是否正确解决了此问题。任何意见或建议都将不胜感激。

  • 谢谢和欢呼。

3 个答案:

答案 0 :(得分:2)

我认为这是错误的:

from datetime import date, timedelta
import os
import win32com.client


path = os.path.expanduser("C:\\Users\\xxxx\\Documents\\Projects\\VBA Projects\\VLOOKUP Automation\\Vlookup File Location")
today = date.today()

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.Folders("xxx").Folders.Item("Inbox")
messages = inbox.Items
subject = "xxx"

dateHigh = date.today() - timedelta(days=1)
dateLow = date.today() - timedelta(days=-1)

max = 2500
for count, message in enumerate(messages):
    if count > max:
        break
    if subject in message.subject and message.senton.date() > dateLow and message.senton.date() < dateHigh:
            attachments = message.Attachments
            num_attach = len([x for x in attachments])
            for x in range(1, num_attach+1):
                attachment = attachments.Item(x)
                attachment.SaveASFile(path + '\\' + str(attachment))

您需要提供一个包含参数而不是字符串的对象。 字符串仅用于定义URL片段(https://en.wikipedia.org/wiki/Fragment_identifier)。

请参见https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.controller.redirecttoaction?view=aspnet-mvc-5.2

您可以尝试:

return RedirectToAction("Register", "Auth", "This user is currently configured with a password");

答案 1 :(得分:1)

您可以尝试以下语法::

[带参数的RedirectToAction]

返回RedirectToAction(“操作”,“控制器”,新的{@ id = id});

答案 2 :(得分:0)

您可以像下面这样自定义ActionMethodSelectorAttribute

public class RequireRequestValueAttribute : ActionMethodSelectorAttribute
{
    public RequireRequestValueAttribute(string valueName)
    {
        ValueName = valueName;
    }

    public override bool IsValidForRequest(RouteContext routeContext, ActionDescriptor action)
    {
        bool contains = false;
        contains = routeContext.HttpContext.Request.Query.ContainsKey(ValueName);   
        return contains;
    }

    public string ValueName { get; private set; }
}

用于测试:

[HttpGet]
public IActionResult Register()
{
    return View();
}
[HttpGet]
[RequireRequestValue("caption")]
public IActionResult Register(string caption)
{
    ViewData["Title"] = "Registration";

    ViewBag.caption = caption;

    return View();
}
[HttpPost]
public async Task<IActionResult> Register()
{
    return RedirectToAction("Register", "Account", new { caption = "This user is currently configured with a password" });
}