1

I have an url like /register/new-user.

If that url has a parameter such as /register/new-user?a=1 execute itself controller, but if that url does not have any parameters, then execute checkuser controller's newuser action.

However I can't use redirect, because I care about Google indexing.

Could you help me?

UPDATE

I used urlrewrite in webconfig and I solved my problem.

<rule name="rulename" stopProcessing="true">
          <match url="^register/newuser$" ignoreCase="false" />
          <conditions>
            <add input="{QUERY_STRING}" pattern="ref=" negate="true" />
          </conditions>
          <action type="Rewrite" url="/checkuser" appendQueryString="false"/>
2
  • I don't think it's possible to have multiple actions with same name. I ran into issues with that in past and had to create different actions. If I am not wrong, unlike calling an overridden method within code with different parameters is not the same as calling an action via URL with different params. You can try adding Route attribute to configure routes with different query string, but not sure that's possible. Commented Dec 17, 2020 at 12:37
  • Something like here? Route to different actions with the same url with different params Commented Dec 17, 2020 at 12:41

2 Answers 2

1

You can use Route attribute to have same url for different actions -

    [HttpGet]
    [Route("register/new-user")]
    public string CheckUser()
    {
        return "Check User";
    }

    [HttpGet]
    [Route("register/new-user")]
    public string NewUser(string a)
    {
        return $"New User - {a}";
    }

You can have n number of same routes as long as all of them different in pattern of their query string.

Look below, all are valid unique routes -

    // /myroute/myaction
    [HttpGet]
    [Route("myroute/myaction")]
    public string DefaultRoute()
    {
        return "Default";
    }

    // /myroute/myaction?arg1=1
    [HttpGet]
    [Route("myroute/myaction")]
    public string RouteWithOneParam(string arg1)
    {
        return $"One Param - {arg1}";
    }

    // /myroute/myaction?arg1=1&arg2=2
    [HttpGet]
    [Route("myroute/myaction")]
    public string RouteWithTwoParam(string arg1, string arg2)
    {
        return $"Two Param - {arg1} and {arg2}";
    }

    // /myroute/myaction/1
    [HttpGet]
    [Route("myroute/myaction/{arg1}")]
    public string AdditionalRouteWithOneParam(string arg1)
    {
        return $"Additional One Param - {arg1}";
    }

    // /myroute/myaction/1/2
    [HttpGet]
    [Route("myroute/myaction/{arg1}/{arg2}")]
    public string AdditionalRouteWithTwoParam(string arg1, string arg2)
    {
        return $"Additional Two Param - {arg1} and {arg2}";
    }
Sign up to request clarification or add additional context in comments.

6 Comments

Firstly thanks for reply. Is it able to for different controllers' actions?
Yes, you can have them in 2 different controllers.
Did you test this? In my experience, it will throw an exception saying multiple routes matched. If it is tested, than it's good.
@pirate, Yes I did. It will throw error if your route is exactly the same. Here, one route has no query parameters and other has query parameters so it works. The router just matches the pattern and tries to find the appropriate action.
@NikhilPatil alrighty, I ran into issues in past. May be I'll give it a shot. Thanks.
|
0

Have you tried Routing? You can look it from below here : https://learn.microsoft.com/en-us/previous-versions/cc668201(v=vs.140)?redirectedfrom=MSDN

Some lines from the page : ASP.NET routing enables you to use URLs that do not have to map to specific files in a Web site. Because the URL does not have to map to a file, you can use URLs that are descriptive of the user's action and therefore are more easily understood by users.

Thus, you can specifically add several routes and give each of them a different controller according to your requirements

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.