Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 326485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:18:49+00:00 2026-05-12T09:18:49+00:00

I’m building an MVC app for the first time. Currently, my app presents a

  • 0

I’m building an MVC app for the first time. Currently, my app presents a small form that will let the user provide an input string (a url) and on submit, will use the user’s input to create a new record within the db table, and output a clean url. I’d like to add a condition within my homecontroller file that will:

1) check if the “url” input already exists within the database table and
2) if so, will display that record verses creating a duplicate record.

    Index View --------------------

        <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

        <html xmlns="http://www.w3.org/1999/xhtml" >
        <head runat="server">
            <title></title>
        </head>
        <body>
            <div>
            <form action="/Home/Create" method="post">
            Enter:  <input type="text" name="urlToShorten" id="shortenUrlInput" />
            <input type="submit" value="Shorten" />
            </form>

            </div>



        </body>
        </html>

    Create View ------------------------------------------------------------

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title></title>
    </head>
    <body>
        <div>
        The clean url:<br />
        <%= string.Format("{0}/{1}",Request.Url.GetLeftPart(UriPartial.Authority),ViewData["shortUrl"]) %>

        </div>
    </body>
    </html>

    Homecontroller----------------------------------------------------------


        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.Mvc;
        using System.Web.Mvc.Ajax;
        using ShortUrl.Models;

        namespace ShortUrl.Controllers
        {
            [HandleError]
            public class HomeController : Controller
            {
                public ActionResult Index()
                {
                    return View();

                }

                [HandleError]
                public ActionResult Create(string urlToShorten)
                {
                    if (string.IsNullOrEmpty(urlToShorten)) 

                    {

                        return RedirectToAction("Index");
                    }



                    else
                    {
                        long result = ShortUrlFunctions.InsertUrl(urlToShorten);
                        ViewData["shortUrl"] = result;
                        return View("Create");
                    }
                }
                [HandleError]
                public ActionResult Resolve(long? id)
                {
                    if (!id.HasValue || id.Value == 0)
                    {
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        string url = ShortUrlFunctions.RetrieveUrl(id.Value);
                        if (url == null)
                        {
                            return RedirectToAction("Index");
                        }
                        else
                        {

                            return Redirect(url);
                        }
                    }
                }
            }
        }

------------ShortUrlFunctions.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ShortUrl.Models
{
   public static class ShortUrlFunctions
   {
       public static string RetrieveUrl(long inputKey)
       {
           using (ShortUrlEntities db = new ShortUrlEntities())
           {


                   var existingUrl = (from t in db.ShortURLSet where
t.id == inputKey select t).Take(1);
                   if (existingUrl.Count() == 1)
                   {
                       return existingUrl.First().url;

                   }
                   else
                   {
                       return null;
                   }
           }
       }

           public static  long InsertUrl(string inputUrl)
           {
               long result = 0;
               if(!string.IsNullOrEmpty(inputUrl))
               {
                   using (ShortUrlEntities db = new ShortUrlEntities())
                   {
                       if (inputUrl.IndexOf(@"://") == -1) inputUrl =
"http://" + inputUrl;
                           ShortURL su = new ShortURL();
                       su.url = inputUrl;
                       db.AddToShortURLSet(su);
                       db.SaveChanges();
                       result = su.id;

           }
       }

               return result;


    }
  }
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:18:50+00:00Added an answer on May 12, 2026 at 9:18 am

    What you need is a method on your ShortUrlFunctions class that can check if a given url exists in your database. If that method is named GetIdForUrl then all you need to do is change your Create action as follows:

            [HandleError]
            public ActionResult Create(string urlToShorten)
            {
                if (string.IsNullOrEmpty(urlToShorten)) 
                {
                    return RedirectToAction("Index");
                }
    
                // No need for an else here since you have a return on the if above.
    
                long result = ShortUrlFunctions.GetIdForUrl(urlToShorten);
    
    
                // I am assuming that the function above returns 0 if url is not found.            
                if (result == 0)
                {
                    result = ShortUrlFunctions.InsertUrl(urlToShorten);
                }
    
                ViewData["shortUrl"] = result;
                return View("Create");
            }
    

    EDIT: (In response to your comment)

    A sample implementation of GetIdForUrl would be:

    public static long GetIdForUrl(string inputUrl) 
    {
        using (ShortUrlEntities db = new ShortUrlEntities())
        {
            var checkUrl = (from t in db.ShortURLSet 
                            where t.url == inputUrl select t.id);
    
            if (checkUrl.Count() == 1) 
            {
                return checkUrl.First();
            }
            else
            {
                return 0;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.